Chat bot
Today, I atteded the seminer at KISA. I got alot of information from this seminer. so , i decided to post on my technical blog.I will post when i will attend seminer. I am so expected about next seminer.
Let's start to review
★ Instructor : Young-Wook Kim, He works on Microsoft
★ lecture material :lecture GitHub
What is chat bot?
[dictionary] : a computer program designed to simulate converstion with human users, especially over the internet.
Chat bot is low entry barrier and has alot of availability.
He recommand to unadvisable to use Chat bot Tool. Beacuse, it is not easy when we want to add more detail function.
so, We have to work coding. Every chat bot platform based to Pizza delivery service beacuase, it is easy to make.
Pizza delivery chat bot service is so simple and easy to develop also, an undemanding work. It is looks like a ‘Hello world’ of Chat bot.
Before, we make a chat bot service. we have to consider about chat bot service level which is must optimizing product. It measn that Don’t expected too much. First of all make simple.
Chat bot used technique
These technics are already made to service. so, if you want to put in your project, You can use the service.
-
Pattern Recognition : number, text, voice, picture can be a pattern. (for example): when you take a picture to your face, face in picture can be a pattern.
-
Natural Language Processing : This is very important things. (for example): when you want to order to chinese restaurant by phone. you can feel like conversation with person. It feels like similar to do the Fourth Industrial Revolution.
-
Text Mining : When you ordered something, you will text on phone or pc whatelse. You might make typing error. but, Text mining technic will help you to find a correct word. (for example) If you write wrong word when you ordered, Text mining technic will find in antonym. It will give to you correct word. order will be success.
these technics are very useful and based on deep learning. deep learning is kinds of machine learning. But, We can’t have to consider about deep learning. we just use the service including these technics. These technics also have a Korean version. so, we can easy to use.
Make a Chatbot
★ This lecture will use the Microsoftware Chatbot Framework
Chat bot is a Web programming. beacuse, When messinger will send a message to web server. web server will proccessing about message. so, Web support program can make a chat bot. But, We have to think about between coding or just use framework.
★ Microsoftware Bot Frame work is made up two languages : C#, node.js
★ Microsoftware Bot Frame work support :
● Skype
● email - only office365 mail
● shopping mall web chat service
● Kik
● Slack
● Telegram
● Twilio - This is Grobal company, they can surpport to text and every mobile phone in the world.
● direct line app integration - Cacao talk, Line ..etc , these will connect to rest chennel.
Each messinger service is differ and going to update service by service. so, web server have to change to process messinger service by messinger service. so, Microsoftware solve this problem.
Fortunately, Connector fee is free. so, we just register our web server url to connector. Connector will help you to process about which messinger chennel. so, if Messinger program will change, it is not a big deal.
But, Web service sould only https. beacuse,privacy security problem. if you want to http,you coudln’t use a cloud service.
◆ Think about web service.
if you want to maintenance of state , what did you do? we use cookie but, chat bot can’t put in cookie so, connector service will be help this.
so, connector has many ID. channel ID, conversation ID, user ID. these ID will help to maintenance of state.
Developing setting
- Visual Studio 2015 or higher
- Visual Studio Bot Project Template //lecture GitHub
-
Don’t release compression
-
File dicrectory: C:\Users\beaut\Documents\Visual Studio 2017\Templates\ProjectTemplates\Visual C#
-
if you can’t find C# folder, you can make yourself.
- Bot Emulator //lecture GitHub
-
[windows] : botframework-emulator-Setup-3.5.31.exe
-
[mac] : botframework-emulator-3.5.31-mac.zip //for node.js user
Make a Chatbot project!!
- open visual studio2017
- File -> new -> project -> visual studio C# -> bot application
- You can find a error : this means that some library or package on web server. so, you have to solution build then every library or package will download.
◈ Web.config : basic setting control
◈ Dialogs : put in your conversation dialog.
RootDialog.cs : Basically, first perform this cs file.
RootDialog.cs
using System;
using System.Threading.Tasks;
using Microsoft.Bot.Builder.Dialogs;
using Microsoft.Bot.Connector;
namespace GreatWall.Dialogs
{
[Serializable]
public class RootDialog : IDialog<object>
{
//first, this method implement.
public Task StartAsync(IDialogContext context)
{
//call MessageReceivedAsync!!
//Wait method : wait to users input.
context.Wait(MessageReceivedAsync);
return Task.CompletedTask;
}
private async Task MessageReceivedAsync(IDialogContext context, IAwaitable<object> result)
{
/*activity : user's every action.*/
var activity = await result as Activity;
/*calculate something for us to return
if) user types "Hello" then, activity.Text means "Hello".*/
int length = (activity.Text ?? string.Empty).Length;
/*return our reply to the user
push to user's messinger.
this example is echo bot.
if you write on your messiner "Hello",
this chat bot will send to your text.*/
await context.PostAsync($ " You sent {activity.Text} which was {length} characters " );
context.Wait(MessageReceivedAsync);
}
}
}
Let's do it Run
1. press F5
2. As i said to you, this is Web coding. so, it will give to you web browser.
3. You can find out IIS web server icon on your windows right down screen. visual studio include IIS express web server. so, this IIS web server help this project result. that’s reason why this project can return to web browser.
4. Now, It’s time to use a Bot Emulator. !!
5. press Connect button You can find out 200 number then, it works successfully.
I will ask them beacause, they said to me it only works on https but, My new example sample works on http. so, I need answer “Why”.
6. press POST link in emulator
It means that every conversation information send and receive to json file.
7. If you want to code editing, you would shut down project run.
8. Emulator is little bit slow, so if you want to more quickly see result, just turn on while your code editing.
example 01 : erasing echo conversation
using System;
using System.Threading.Tasks;
using Microsoft.Bot.Builder.Dialogs;
using Microsoft.Bot.Connector;
namespace GreatWall.Dialogs
{
[Serializable]
public class RootDialog : IDialog<object>
{
public Task StartAsync(IDialogContext context)
{
context.Wait(MessageReceivedAsync);
return Task.CompletedTask;
}
private async Task MessageReceivedAsync(IDialogContext context, IAwaitable<object> result)
{
var activity = await result as Activity;
string message = string.Format("{0}을 주문 하셨습니다. 감사합니다." ,activity.Text);
// return our reply to the user
await context.PostAsync(message);
context.Wait(MessageReceivedAsync);
}
}
}
1. Before find out the result, Press the New Conversation button in Emulator.
example 01 : Result
example 02 : Hi function01
-
[Serializable] : Every class will output as a json file in chatbot frameworks. so, that’s reason why we must declare to Serialization.
-
IDialog
-
public Task StartAsync(IDialogContext context) : This Method part is a dialog start point to Chat bot.
using System;
using System.Threading.Tasks;
using Microsoft.Bot.Builder.Dialogs;
using Microsoft.Bot.Connector;
namespace GreatWall.Dialogs
{
[Serializable]
public class RootDialog : IDialog<object>
{
public Task StartAsync(IDialogContext context)
{
context.Wait(MessageReceivedAsync);
return Task.CompletedTask;
}
private async Task MessageReceivedAsync(IDialogContext context, IAwaitable<object> result)
{
await context.PostAsync("안녕하세요 신속배달 만리 장성 봇입니다. 주문하시려는 음식을 입력해 주세요");
context.Wait(SendWelcomeMessageAsync);
}
private async Task SendWelcomeMessageAsync(IDialogContext context, IAwaitable<object> result)
{
var activity = await result as Activity;
string message = string.Format("{0}을 주문하셨습니다. 감사합니다.", activity.Text);
await context.PostAsync(message);
//context.Call(new NameDialog(),this.NameDialogResumeAfter);
//Recursive call, so we need to exit point.
context.Wait(SendWelcomeMessageAsync);
}
}
}
example 02 : Result
example 03 : new Dialog implement
-
We can’t see a Connector which helps to connect with every messinger in cloud system.
-
user sending is Activity.
-
Message is user sending text which is inside activity. message can include image.
-
Activity type :
▶ message : Sent when general content is passed to or from a user and a bot.
▶ Converation Update : Sent when the Converation’s properties change, for exampe the topic name, or when user joins or leaves the group.
▶ Contract Relation Update : Sent when bot added or removed to contact list.
▶ Typing : Sent When a user is Typing.
▶ Ping : Send when a keep-alive is needed.
but, some messinger surpport these thing or not. so, we usually do not use this function.
if you have to use these function, Go to the Controller folder and open a MessagesController.cs file.
Actually, Before start to Dialog folder, Controller folder start first.
MessagesController.cs
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using System.Web.Http;
using Microsoft.Bot.Builder.Dialogs;
using Microsoft.Bot.Connector;
namespace GreatWall
{
[BotAuthentication]
public class MessagesController : ApiController
{
/// <summary>
/// POST: api/Messages
/// Receive a message from a user and reply to it
/// </summary>
// web 통신
public async Task<HttpResponseMessage> Post([FromBody]Activity activity)
{
if (activity.Type == ActivityTypes.Message)
{
/*new dialog is RootDialog(). so,RootDialog start first.*/
await Conversation.SendAsync(activity, () => new Dialogs.RootDialog());
}
else
{
//except message. other things.
HandleSystemMessage(activity);
}
var response = Request.CreateResponse(HttpStatusCode.OK);
return response;
}
private Activity HandleSystemMessage(Activity message)
{
if (message.Type == ActivityTypes.DeleteUserData)
{
// Implement user deletion here
// If we handle user deletion, return a real message
}
else if (message.Type == ActivityTypes.ConversationUpdate)
{
// Handle conversation state changes, like members being added and removed
// Use Activity.MembersAdded and Activity.MembersRemoved and Activity.Action for info
// Not available in all channels
}
else if (message.Type == ActivityTypes.ContactRelationUpdate)
{
// Handle add/remove from contact lists
// Activity.From + Activity.Action represent what happened
}
else if (message.Type == ActivityTypes.Typing)
{
// Handle knowing tha the user is typing
}
else if (message.Type == ActivityTypes.Ping)
{
}
return null;
}
}
}
Best is making dialog , Don’t depends on these api.
example 03 : code
[RootDialog]
using System;
using System.Threading.Tasks;
using Microsoft.Bot.Builder.Dialogs;
using Microsoft.Bot.Connector;
namespace GreatWall.Dialogs
{
[Serializable]
public class RootDialog : IDialog<object>
{
string welcomeMessage = "안녕하세요 만리장성 봇 입니다. 1.주문, 2. FAQ 중에 선택하세요";
public Task StartAsync(IDialogContext context)
{
context.Wait(MessageReceivedAsync);
return Task.CompletedTask;
}
private async Task MessageReceivedAsync(IDialogContext context, IAwaitable<object> result)
{
await context.PostAsync(welcomeMessage);
context.Wait(SendWelcomeMessageAsync);
}
private async Task SendWelcomeMessageAsync(IDialogContext context, IAwaitable<object> result)
{
var activity = await result as Activity;
//erase space text
string selected = activity.Text.Trim();
if(selected =="1")
{
await context.PostAsync("음식 주문 메뉴 입니다. 원하시는 음식을 입력해 주십시오.");
//other dialog call
context.Call(new OrderDialog(), DialogResumeAfter);
}else if(selected == "2")
{
await context.PostAsync("FAQ서비스 입니다. 질문을 입력해 주십시오.");
context.Call(new FAQDialog(), DialogResumeAfter);
}
else
{
await context.PostAsync("잘못 선택 하셨습니다. 다시 선택해 주십시오.");
context.Wait(SendWelcomeMessageAsync);
}
}
private async Task DialogResumeAfter(IDialogContext con text, IAwaitable<string> result)
{
try
{
string message = await result;
await this.MessageReceivedAsync(context, result);
}
catch (TooManyAttemptsException)
{
await context.PostAsync("오류가 생겼습니다. 죄송합니다.");
}
}
}
}
◆ Add class -> make a OrderDialog.cs file.
◆ Add class -> make a FAQDialog.cs file.
[OrderDialog]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Threading.Tasks;
using Microsoft.Bot.Connector;
using Microsoft.Bot.Builder.Dialogs;
namespace GreatWall.Dialogs
{
[Serializable]
public class OrderDialog : IDialog<string>
{
public Task StartAsync(IDialogContext context)
{
context.Wait(MessageReceivedAsync);
return Task.CompletedTask;
}
private async Task MessageReceivedAsync(IDialogContext context, IAwaitable<object> result)
{
var activity = await result as Activity;
if (activity.Text.Trim() == "그만")
{
context.Done("주문완료");
}
else
{
string message = string.Format("{0}을 주문하셨습니다. 감사합니다.", activity.Text);
await context.PostAsync(message);
context.Wait(MessageReceivedAsync);
}
}
}
}
[FAQDialog]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Threading.Tasks;
using Microsoft.Bot.Connector;
using Microsoft.Bot.Builder.Dialogs;
namespace GreatWall.Dialogs
{
[Serializable]
public class FAQDialog : IDialog<string>
{
public Task StartAsync(IDialogContext context)
{
context.Wait(MessageReceivedAsync);
return Task.CompletedTask;
}
private async Task MessageReceivedAsync(IDialogContext context, IAwaitable<object> result)
{
var activity = await result as Activity;
if (activity.Text.Trim() == "그만")
{
context.Done("주문완료");
}
else
{
await context.PostAsync("FAQ Dialog 입니다.");
context.Wait(MessageReceivedAsync);
}
}
}
}
-
“DialogResumeAfter” Method : After ohter dialog call and back, designate what will do root dialog. We must make this method. beacuse, When we come back to root dialog, something error will happen.
-
sometimes, Emulator confused conversation, so, if you want to clear test, you have to choose New conversation button. Before, clean up last conversation, keep testing again.
example 03 : Result
In my opinion
They want to use another framework for chatbot QnA section.so, i will not to keep positing it.
Maybe, i will put chatbot in my project. so, That will be keep going on posting.
Although, I was in Busan, Some seminar was in Busan same story telling. but i want to know about Chat bot. so, i checked in Busan.
Either i will not positing it. beacuse, They just want to show us to use a microsoft merchandise. I felt it was just advertising.
I will posting anonther seminar or forum. I am so expected.
bonus lecture section
※ Line message service is useful at Japan
※ If your company aims is Grobal, you must choose to use CaCao talk service.
※ Whatever you must support to Skype, Facebook, Whatsapp.