0

I am currently using react.js on the client side and node.js on the server side.The Webchat client communicates with the bot using directline. Is there a way to send out data from the bot to the client?

The data here are just an array of messages. I wish to initialise the bot with previous conversation messages. I tried sendConversationHistory() method but that doesn't seem to work well on deployment. Hence, I am thinking of fetching messages from the database, and then sending these obtained messages to the client.

I came across the backchannel approach where we can post activity from the client.I plan to post an activity called getHistory() from the Webchat client and when the server listens for this activity, it fetches the messages from the database and sends them as a response to the client.The client reads this response and renders accordingly.I plan to send data from the bot to the client,but I am unsure about the correct methods and syntax to use for sending this data from the bot.

Client end:

let messages;
function getMessages()  {
botConnection
    .postActivity({type: "event", value: "", from: {id: "me" }, name: "getHistory"})
    .subscribe(res => messages = res);
}

I am not sure how will I proceed with the server end.

bot.on("event", function (event) {
let fetchedMessages = [];
fetchedMessages = fetchConversations(); /*a method which fetches the messages from the database*/
bot.send(fetchedMessages) ; /*unsure about this part*/   
}

I am not sure about how the fetchedMessages would be sent to the client.I want the messages variable in client to be equal to fetchedMessages of the server.I am a beginner in javascript and still familiarizing with the bot framework. Thanks for reading!

Smit Shah
  • 21
  • 1
  • 6
  • Did you consider using directline as well for the conversastion history? https://stackoverflow.com/questions/56969008/is-there-a-way-to-persist-chat-history-in-ms-bot-framework-web-chat-after-a-page – Hessel Jul 23 '20 at 11:13
  • I had tried something similar,but I got an error.I will try this and let you know.Just a quick follow-up : I need to show all previous conversations for a particular user..How do I get that? Currently, I am making a query to the database on the back-end and am planning to send those to the front-end.I cant't make a call to the database from the front-end.There's some security policy which doesn't allow any calls to the database from the front-end. TLDR: Need all conversation ids for viewing all conversations, but can't make calls to the db on front-end – Smit Shah Jul 23 '20 at 12:25
  • I do not think conversations are stored by the framework. Just a limited part of it to address stability issues wrt connection (user looses internet connection by accident and need to continue conversation). That part is retrievable by using the sample that I shared. If you need all conversations per user, you probably need to write some middleware and use your own storage. I am pretty sure there are some samples about this somewhere – Hessel Jul 23 '20 at 13:00
  • I have my messages stored for each user in a database.I am fetching a user's messages and just wish to send it across.I mentioned the pseudo code in the question for the same.It's just that I wanted to know the exact format and approach.I did not find any samples for sending this data across.It would be great if you could share some samples. – Smit Shah Jul 23 '20 at 13:59

1 Answers1

0

To send information (that's not a response) from bot to users, you should use proactive messages. Sample here. You can modify what the trigger should be, on when to have the messages sent.

Dana V
  • 935
  • 4
  • 10