Building multi lingual and empatic bots - Sander van den Hoven - Codemotion Amsterdam 2017
-
Upload
codemotion -
Category
Technology
-
view
528 -
download
0
Transcript of Building multi lingual and empatic bots - Sander van den Hoven - Codemotion Amsterdam 2017
using Microsoft.Bot.Connector;
[BotAuthentication]
public class MessagesController : ApiController
{
public async Task<HttpResponseMessage> Post([FromBody]Activity activity)
{
if (activity.Type == ActivityTypes.Message)
{
ConnectorClient connector = new ConnectorClient(new Uri(activity.ServiceUrl));
var replyMSG = $"You said {activity.Text}.");
Activity reply = activity.CreateReply(replyMSG);
await connector.Conversations.ReplyToActivityAsync(reply);
}
else
HandleSystemMessage(activity);
return Request.CreateResponse(HttpStatusCode.OK);
}
}
}
var restify = require('restify');
var builder = require('../../core/');
// Setup Restify Server
var server = restify.createServer();
server.listen(process.env.port || process.env.PORT || 3978, function () {
console.log('%s listening to %s', server.name, server.url);
});
// Create chat connector for communicating with the Bot Framework Service
var connector = new builder.ChatConnector({appId: process.env.MICROSOFT_APP_ID, appPassword: process.env.MICROSOFT_APP_PASSWORD
});
// Listen for messages from users
server.post('/api/messages', connector.listen());
// Create your bot with a function to receive messages from the user
var bot = new builder.UniversalBot(connector, function (session) {
var replyMSG = "You said: %s", session.message.text;session.send(replyMSG);
}); https://github.com/Microsoft/BotBuilder/blob/master/Node/examples/hello-ChatConnector
API: “INVITE GUEST FOR TOMORROW”
https://westus.api.cognitive.microsoft.com/luis/v2.0/apps/
<subscription>?subscription-
key=<key>&timezoneOffset=0&verbose=true&
q=register%20guest%20for%20tomorrow
{
"query": "register a guest for tomorrow",
"topScoringIntent": {
"intent": "MSFTNLBot.RegisterGuest",
"score": 0.9989265,
"actions": [
{
"triggered": true,
"name": "MSFTNLBot.RegisterGuest",
"parameters": []
}
]
},
"intents": [
...
{
"intent": "MSFTNLBot.ParkingPlaces",
"score": 0.0008436582
},
},
...
],
"entities": [
{
"entity": "tomorrow",
"type": "builtin.datetime.date",
"startIndex": 21,
"endIndex": 28,
"resolution": {
"date": "2017-05-17"
}
}
]
}
http://luis.ai
Config & Train:
Outcome:
Detect
Language
Store
Language
in bot
state
Translate
to English
Do
actions
based on
English
Create
Response
Translate
to original
language
Respond
translate
response
{
await SendTyping(activity);
var translator = new Translator();
var lang = translator.Detect(activity.Text);
//set User in State
StateClient stateClient = activity.GetStateClient();
BotData userData = await stateClient.BotState.GetUserDataAsync(activity.ChannelId, activity.From.Id);
userData.SetProperty<string>("originalLanguage", lang);
await stateClient.BotState.SetUserDataAsync(activity.ChannelId, activity.From.Id, userData);
activity.Text = translator.Translate(activity.Text, lang, "en");
var dialog = new CustomLuisDialogML();
await Conversation.SendAsync(activity, () => dialog);
}
[LuisIntent("Greeting")]
public async Task Greeting(IDialogContext context, LuisResult result)
{
var lang = OriginalLanguage;
context.UserData.TryGetValue<string>("originalLanguage", out lang);
string msg = "Hi, nice that you contacted the PA of Sander. What can I do for you?\n";
string speakmsg = msg;
msg += "You can ask me where Sander is and how the weather is at his location.";
var translatedMsg = Translator.GetTransMsg(msg, lang);
await context.SayAsync(translatedMsg, speakmsg);
context.Wait(MessageReceived);
}
string msg = "";var sentimentScore = await Psychologist.GetSentiment(result.Query);if (sentimentScore > 0.7) //Positive{
msg = $"That's great to hear!";}else if (sentimentScore < 0.3) //Negative{
msg = $"I'm sorry to hear that. I hope that we can improve.";}else //No opinion{
msg = $"I see, Will to try to get better.";}
I want to book room
for tomorrow at 9
Room 123 is available.
Shall I book it?
Yes
Room is booked for
you
Show my VMs
You have the following
5 VMs …
… [time passes]
VM ‘Linux01’ runs out of Mem.
Shall I increase Mem size to 8 GB?
Yes
VM ‘Linux01’ is upgraded and runs
smoothly again.
Bot
I want to book my
time for this week Ok, What hours for
Monday?
8 for Project XBooked. What hours
for Monday?
6 for Project X, 2
for Meetings Your new PC has arrived, do
you want to pick it up?
Huh %-)?
Proactive
Message
Message
Handler
Chat Conversation
BotUserDialog Stack
Proactive Bot
Random Message "hi"
Error "Start Dialog"
"\Start TimeSheet" Start Dialog
"Add 8 hour for today"
Sent Reply "Hours Added"
Reply "Dialog Started"
Check if dialog exist
Return True
Check if dialog exist
Return False
Sent Normal Notification "Your coffee is ready?"
Stack Notification 1
Sent Interrupt Notification "Building is on Fire"
Stack Notification "Fire"
"\Stop"
Remove Dialog
loop while notifications
Get Paused Notification
Notification
Sent Notification
Get Paused Notification
Notification
You need a dialog manager
http://github.com/microsoft/botbuilder
http://developer.microsoft.com/cortana
https://github.com/alyssaong1/Bot-Framework-HOL