Supply history on startup
Some folks are interested in initializing WebChat with a history.
Since DirectLine doesn't archive messages, the bot would have to archive them separately.
Here are a couple of possible approaches:
- The web page could get the archived message out of band, e.g. by calling an endpoint provided by the bot. Then passes those to a new property and/or API in WebChat.
- The Bot Framework could enable a hook on the channel side where the bot supplies an
archiveProviderof some kind. If it exists, DirectLine sends those messages to the WebChat. This has the advantage of working with any deployment of WebChat out of the box, but requires a lot of new plumbing in the Bot Framework.
This is clearly half-baked so I'm opening up a dialog.
Maybe they can send in a JSON serialized list of messages in the initial options ?
@danmarshall Yes that's the first approach.
@billba & @danmarshall is it possible somehow to send in a JSON serialized the list of messages to the chat component? If yes, can you tell me please on what props? Or is there another method? I keep the messages in DB, but I want to display them when the chat is initialized. Thanks!
No. The purpose of this proposal is to measure demand for such a feature, and discuss exactly how it might be exposed to the developer.
We're also interested in such a feature. Would save us some time since we don't have to render the history somewhere else and the UX is much better. Please let us know if you plan on supporting this feature or give us some tips on how to do it ourselves. Thanks ;)
@billba @danmarshall I know you guys are trying to figure out a solution that integrates nicely with all the botframework systems, but we are doing message saving ourselves and we just need to be able to pass previous messages to the chat component using props. We're actually thinking about hacking the component ourselves since we need that asap. What would be the best approach? Do you think we could just extend it and get the job done in the constructor, or we should start from source and rebuild it ourselves?
Thanks
I don't think anyone in Microsoft is actively working on this feature. It is a great candidate for a contribution from the community.
In broad strokes the idea is to add a property to ChatProps containing an array of messages and use that to initialize the activities field of the history store. The devil, as always, will be in the details.
@billba I was amazed how straightforward it was to build this feature by wrapping Chat component in an obvious SuperChat component that overrides the constructor and dispatches Receive_Message action for each message from history.
class SuperChat extends Chat {
constructor(props) {
super(props);
const history = this.buildHistory(props.conversation, props.user);
history.forEach(activity => {
this.store.dispatch({
type: 'Receive_Message',
activity
});
});
}
}
To build the proper activity object I used react inspector to see how the actual messages were creating their activity object.
Works flawlessly so far.
Perhaps this is a candidate for a sample since it's not integrated directly into WebChat
What do you think?
Hah! I never would have considered doing it that way! Why not build it into Chat itself?
I am using webchat and directline and i am hoping to achieve the same functionality. The chat bot is embedded as an iframe on the footer of my website. When i navigate across to another tab , i would like to retain the chat history. Can anyone please help if they were able to achieve similar functionality using webchat?
+1. Option of having pagination with scroll would be great too.
@matts310 This seems like a very different issue. How would this be different than the built-in pagination you get with every OS?
@billba There may be a lot of history being loaded at startup. It may be better to load chunks either from memory or external requests when scrolling to older portions of the history. I was envisioning this through some form of pagination.
My current approach is from the web "push notification" in the browsers, actually, rather than from the chat framework itself. This WebChatPush project can recall chat history from previous windows by clicking on desktop push notifications: https://github.com/doristchen/WebChatPush
@ancashoria thanks. Could you give more details on how its done exactly? Maybe give a link to a forked repo with your code?
In addition, how would I set the WebChat session ID? I mean, besides manually adding the messages, like @ancashoria did, I want the context to be continued, rather than re-start a new conversation. Any suggestion?
@morphaxceed here's the full code:
import { Chat } from 'botframework-webchat'
export interface IMessage {
from: string,
text: string,
attachments?: any[],
date?: string,
}
class SuperChat extends Chat {
constructor(props: IProps) {
super(props)
const history = this.buildHistory(
props.conversation.messages,
0,
props.conversation.customerAddress,
props.user.id
)
this.injectMessages(history)
}
buildHistory(messages, startIndex, customerAddress, agentId) {
const conversationId = customerAddress.conversation.id
const customerName = customerAddress.user.name
return messages.map((message, messageIndex) => ({
type: "message",
text: message.text,
from: {
id: message.from === 'agent' || message.from === 'bot' ? agentId : '',
name: message.from === 'user' ? customerName : message.from
},
attachments: message.attachments,
timestamp: message.date,
id: `${conversationId}|${startIndex + messageIndex}`,
}))
}
injectMessages(messages: any[]) {
messages.forEach(activity => {
this.store.dispatch({
type: 'Receive_Message',
activity
})
})
}
}
customerAddress is created by botframework and you can get it from session object
Here's the interface for customerAddress https://docs.botframework.com/en-us/node/builder/chat-reference/interfaces/botbuilder_d.iaddress.html
In buildHistory() I do extra checks that you don't need. Make sure you return the same object structure and you should be fine.
Also, this code is tested only on webchat version 0.10.8
Hope it helps
Prefer option 1 and we persist our whole Redux store.
@billba, any updates?
You can add the conversationId to the BotChat component in react:
ReactDOM.render(
<BotChat.Chat
chatTitle={false}
rootId='apd-form'
bot={{
id: 'BOT_ID',
name: 'Porter'
}}
user={{
id: 'USER_ID',
name: 'Sam'
}}
directLine={{
// To continue with a previous conversation we need to pass the conversationId, this isn't needed when we start a new one
conversationId: 'DXhOx67kZVB8Rmc83BrvLi',
// For now we are using the secret from DirectLine but moving forward we'll need to get a token
secret: 'XXXXX',
// domain: params.domain,
// token: params.t,
// In order to receive the history on a previous conversation we can't use polling for now
webSocket: false
}}
locale={ params.locale }
resize="detect"
// sendTyping={ true } // defaults to false. set to true to send 'typing' activities to bot (and other users) when user is typing
/>,
document.getElementById('BotChatGoesHere')
);
It remembers the last step and continues on. If you turn off websockets it also loads the entire history of the chat, although I'm unsure how long this will be stored for as it doesn't appear to be stored in my Azure state.
Would be great for some guidance as to whether this history is being pulled from and when it will expire and also whether we can make it work with websockets.
P.S. Ignore the 'rootId' - I've created a custom build to accept an initial message to I can trigger a dialog in the bot automatically.