Reputation: 47
[Question pertains to the Microsoft Bot Framework]
There is certain information about the user that my bot will need, but I don't want the user himself to submit it because
Essentially, my chat bot is embedded in a web page. There is information in each session of that web page that I need to submit to the corresponding chat bot controller instance without an explicit message becoming visible in the chat window.
How do I pass information to the chat bot controller? If I need the conversation Id to address the bot, how do I get it programmatically inside of my web page Javascripts? Right now, my chat bot is integrated into the web page with the simple iframe line that can be found in the docs
http://docs.botframework.com/connector/embed-chat-control/#navtitle
Example Scenario : My chat bot is a chat window on a facebook page. I need the chat bot to know who the user is and say, for instance, 'Hello username' without any prompting to get username
Upvotes: 1
Views: 5273
Reputation: 11
I was able to pass the user ID and name from a web application to a C# Microsoft Bot Framework bot that is embedded within an iframe. The user ID and name can be passed in the iframe like this (substitute your bot's URL and secret, and whatever user ID and name you want to pass:
<iframe src="https://webchat.botframework.com/embed/assistant-bot?s=[bot_secret]&userid=[user_id]&username=[user_name]"></iframe>`
In my bot, the turn context is used to access the user ID and name, and is available in methods OnMessageActivityAsync(ITurnContext<IMessageActivity> turnContext, CancellationToken cancellationToken)
in class DialogBot.cs
, and in OnMembersAddedAsync(IList<ChannelAccount> membersAdded, ITurnContext<IConversationUpdateActivity> turnContext, CancellationToken cancellationToken)
in class QnABot.cs
.
In these methods, turnContext.Activity.From.Id
contains the passed-in user ID, and turnContext.Activity.From.Name
contains the passed-in user name.
I believe the bot should also be able to access user ID and user name in the same way as above if you embed the bot with DirectLine as in the below code snippet from Secured WebChat Control that was sent to me, but I haven't had the chance yet to try it:
$.get('/api/token', function (token) {
window.WebChat.renderWebChat({
directLine: window.WebChat.createDirectLine({ token: token }),
userID: '[user_id]',
username: '[user_name]',
locale: 'en-us',
styleOptions: { hideUploadButton: true }
}, document.getElementById('webchat'));
}
Upvotes: 1
Reputation: 46
In that case you have to create your own conversation window that allows you to post and get the Message object.
Index.html
<script src="script.js">
<ul class="chat" id="conversation">
</ul>
<input id="input-text" type="text" />
<button type="button" id="btn-send">Send</button>
script.js
$('#btn-send').click(function () {
// Convert text to Message object
var message = {
text: $('#input-text').val(),
type: "Message"
};
// Create outgoing message html and add to list
var htmlOutgoing = "<li>" + message.text + "</li>";
$('#conversation').append(html);
$.ajax({
url: 'api/Messages/Post', // Change to your proper url
dataType: "json",
type: "POST",
contentType: 'application/json; charset=utf-8',
data: JSON.stringify(message),
async: true,
processData: false,
cache: false,
success: function (data) {
// Create incoming message html and add to list
var htmlIncoming = "<li>" + data.text + "</li>";
$('#conversation').append(htmlIncoming);
},
error: function (xhr) {
alert('error');
}
});
$('#text-input').val("");
})
I hope, this would help you.
Upvotes: 1
Reputation: 46
Microsoft Bot Framework uses Message
class to exchanges messages between/among your bot and users. It comes with different properties. There are some which are filled by the Framework and depending on the Framework. However, there are few that allows you to control the state of the conversation.
BotUserData
, BotConversationData
and BotPerUserInConversationData
are the one that allow you to exchange dynamic data. Each of three has their own significance. Checkout official documentation for details.
To get information regarding different channel, use ChannelData
that contains the original message coming from the channel (Facebook)
Upvotes: 0