Azure Bot Serviceを使って、15分で「りんな」みたいなチャットボットを作ってみる

最近話題となっている、マイクロソフトのりんなですが、自分でも同じものを作ってみたいと思ったことありませんか?今回はりんなみたいなチャットボットを15分で作成する方法をご紹介します。
試されたい方はこちらから:
Skypeでもお試しいただけますよ!
準備するもの:
- Microsoft Account
- Skype(なくても一応試せますが、Skypeの方がやった感があります)
- Azureサブスクリプション
- チャットボットアカウント ※
※User Localの「人工知能チャットボット」をAzure Bot Serviceと組み合わせてやっています。なので、Azure側はあくまでもメッセージのやり取りのみをやっていて、裏側でメッセージを考えているのは、このUser LocalのボットAPIです。登録はこちらからできます: http://ai.userlocal.jp/document/free/top

作成手順
-
まず、Azure Portalから、「新規」>「Intelligence + Analytics」>「Bot Service」の順に選択し、項目を記入し、「作成」をクリックします。
-
作成されたものは「App Service」からアクセスします。
-
アプリの種類は「Bot Service」として表示されます。これをクリックします。
-
開くと、Microsoft App IDの作成が求められます。「Create Microsoft App ID and Password」をクリックします。
-
アプリ名は(1)で設定したアプリ名、アプリIDは自動で生成されます。「アプリパスワードを生成して続行」をクリックします。
-
パスワードが表示されます。表示されたパスワードをメモ帳等のテキストエディタへ貼り付けて、「OK」をクリックします。パスワードは後程利用します。
-
「終了してボットのフレームワークに戻る」をクリックします。
-
先ほど表示されていた、App IDとメモ帳へ貼り付けていたパスワードを入力します。
-
次に言語を選びます。今回はC#で作りますので、「C#」をクリックします。
-
次に、作成するBOTのテンプレートを選びます。今回は、Basicを選択しますが、ほかにも色々用意されています:
Basic – 単純に入力した内容をオウム返しするBOTです。
Form – FormFlowを利用してアンケートを取るBOTが作成できます。
Proactive – Azure Functionsのトリガーイベントで、Azure Botに動作させるテンプレートです。
Language understanding – 自然言語解析のLUISを組み合わせたテンプレートです。 -
クリックすると、BOTの作成が始まります。1分ぐらいで完了します。
-
これで、Botは作成されました。右側のチャット画面からメッセージを入力すると、会話ができますが、まだこのままだと送ったメッセージの桁数しか教えてくれませんので、run.csxのコードを書き換えます。
Run.csx
#r "Newtonsoft.Json"
#load "EchoDialog.csx"
using System;
using System.Net;
using System.Threading;
using Newtonsoft.Json;
using Microsoft.Bot.Builder.Azure;
using Microsoft.Bot.Builder.Dialogs;
using Microsoft.Bot.Connector;
public static async Task<object> Run(HttpRequestMessage req, TraceWriter log)
{
log.Info($"Webhook was triggered!");
// Initialize the azure bot
using (BotService.Initialize())
{
// Deserialize the incoming activity
string jsonContent = await req.Content.ReadAsStringAsync();
var activity = JsonConvert.DeserializeObject<Activity>(jsonContent);
// authenticate incoming request and add activity.ServiceUrl to MicrosoftAppCredentials.TrustedHostNames
// if request is authenticated
if (!await BotService.Authenticator.TryAuthenticateAsync(req, new [] {activity}, CancellationToken.None))
{
return BotAuthenticator.GenerateUnauthorizedResponse(req);
}
if (activity != null)
{
// one of these will have an interface and process it
switch (activity.GetActivityType())
{
case ActivityTypes.Message:
await Conversation.SendAsync(activity, () => new EchoDialog());
break;
case ActivityTypes.ConversationUpdate:
var client = new ConnectorClient(new Uri(activity.ServiceUrl));
IConversationUpdateActivity update = activity;
if (update.MembersAdded.Any())
{
var reply = activity.CreateReply();
var newMembers = update.MembersAdded?.Where(t => t.Id != activity.Recipient.Id);
foreach (var newMember in newMembers)
{
reply.Text = "Welcome";
if (!string.IsNullOrEmpty(newMember.Name))
{
reply.Text += $" {newMember.Name}";
}
reply.Text += "!";
await client.Conversations.ReplyToActivityAsync(reply);
}
}
break;
case ActivityTypes.ContactRelationUpdate:
case ActivityTypes.Typing:
case ActivityTypes.DeleteUserData:
case ActivityTypes.Ping:
default:
log.Error($"Unknown activity type ignored: {activity.GetActivityType()}");
break;
}
}
return req.CreateResponse(HttpStatusCode.Accepted);
}
}
EchoDialog.csx
次に、EchoDialog.csxのコードも書き換えます。
#r "Newtonsoft.Json"
using System;
using System.Threading.Tasks;
using Microsoft.Bot.Builder.Dialogs;
using Microsoft.Bot.Connector;
using System.Net;
using System.Threading;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
// For more information about this template visit http://aka.ms/azurebots-csharp-basic
[Serializable]
public class EchoDialog : IDialog<object>
{
protected int count = 1;
public Task StartAsync(IDialogContext context)
{
try
{
context.Wait(MessageReceivedAsync);
}
catch (OperationCanceledException error)
{
return Task.FromCanceled(error.CancellationToken);
}
catch (Exception error)
{
return Task.FromException(error);
}
return Task.CompletedTask;
}
public virtual async Task MessageReceivedAsync(IDialogContext context, IAwaitable<IMessageActivity> argument)
{
var message = await argument;
String chatresponse = null;
//Build the URI
Uri UriBase = new Uri("https://chatbot-api.userlocal.jp");
var builder = new UriBuilder($"{UriBase}/api/chat?");
using (WebClient webclient = new WebClient())
{
//Set the encoding to UTF8
webclient.Encoding = System.Text.Encoding.UTF8;
//Add the question as part of the body
var postBody = $"{{\"message\": \"{message.Text}\", \"key\": \"***ここにUserLocaleさんのAPIキーを入れます***\"}}";
//Add the subscription key header
webclient.Headers.Add("Content-Type", "application/json");
var responseString = webclient.UploadString(builder.Uri, postBody);
chatresponse = JObject.Parse(responseString)["result"].ToString();
}
await context.PostAsync($"{chatresponse}");
context.Wait(MessageReceivedAsync);
}
}
これで出来上がりです!試してみましょう。















コメントを残す