Skip to content

Commit 839a4c9

Browse files
author
Nathan Zylbersztejn
authored
Merge pull request #53 from gausie/send-message-on-land
Automatically send message to existing session on page load if instructed
2 parents 081886e + c0c2749 commit 839a4c9

File tree

5 files changed

+25
-2
lines changed

5 files changed

+25
-2
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
## 0.5.4
2+
- When reconnecting to an existing chat session, the bot will send a message contained in the localStorage key specified by the `NEXT_MESSAGE` constant. The message should be stringified JSON with a `message` property describing the message and an `expiry` property set to a UNIX timestamp in milliseconds after which this message should not be sent.
3+
14
## 0.5.3
25
- Added the parameter hideWhenNotConnected to not display the widget when the server is not connected (defaults to true)
36
- Fixed issue where the 'connected' property was being loaded from previous session instead of being triggered on actual connection

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,10 @@ emit('bot_uttered', message, room=socket_id)
208208
`storage: "local"` defines the state to be stored in the local stoage. The local storage persists after the the browser is closed, and is cleared when the cookies of the browser are cleared, or when `localStorage.clear()`is called.
209209

210210

211+
### Sending a message on page load
212+
213+
When reconnecting to an existing chat session, the bot will send a message contained in the localStorage key specified by the `NEXT_MESSAGE` constant. The message should be stringified JSON with a `message` property describing the message and an `expiry` property set to a UNIX timestamp in milliseconds after which this message should not be sent. This is useful if you would like your bot to be able to offer your user to navigate around the site.
214+
211215

212216
## API
213217

src/components/Widget/components/Conversation/components/Messages/components/Message/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class Message extends PureComponent {
1212
<div className={sender}>
1313
<div className="message-text" >
1414
{sender === 'response' ? (
15-
<ReactMarkdown className={'markdown'} source={text} linkTarget={(url) => { if (!url.startsWith('mailto')) return '_blank'; }} />
15+
<ReactMarkdown className={'markdown'} source={text} linkTarget={(url) => { if (!url.startsWith('mailto') && !url.startsWith('javascript')) return '_blank'; }} transformLinkUri={null} />
1616
) : (
1717
text
1818
)}

src/components/Widget/index.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import {
2222
import { isSnippet, isVideo, isImage, isQR, isText } from './msgProcessor';
2323
import WidgetLayout from './layout';
2424
import { storeLocalSession, getLocalSession } from '../../store/reducers/helper';
25-
import { SESSION_NAME } from 'constants';
25+
import { SESSION_NAME, NEXT_MESSAGE } from 'constants';
2626

2727
class Widget extends Component {
2828

@@ -71,6 +71,20 @@ class Widget extends Component {
7171
storeLocalSession(storage, SESSION_NAME, remote_id);
7272
this.props.dispatch(pullSession());
7373
this.trySendInitPayload()
74+
} else {
75+
// If this is an existing session, it's possible we changed pages and want to send a
76+
// user message when we land.
77+
const nextMessage = window.localStorage.getItem(NEXT_MESSAGE);
78+
79+
if (nextMessage !== null) {
80+
const { message, expiry } = JSON.parse(nextMessage);
81+
window.localStorage.removeItem(NEXT_MESSAGE);
82+
83+
if (expiry === 0 || expiry > Date.now()) {
84+
this.props.dispatch(addUserMessage(message));
85+
this.props.dispatch(emitUserMessage(message));
86+
}
87+
}
7488
}
7589
});
7690

src/constants.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ export const MESSAGES_TYPES = {
2323
CUSTOM_COMPONENT: 'component'
2424
};
2525

26+
export const NEXT_MESSAGE = 'mrbot_next_message';
27+
2628
export const PROP_TYPES = {
2729

2830
MESSAGE: ImmutablePropTypes.contains({

0 commit comments

Comments
 (0)