Go to list of users who liked
Share on X(Twitter)
Share on Facebook
More than 5 years have passed since last update.
Bot Builder v4 でボット開発 : 最小限構成でまずはオウム返しボットを作る
この記事では Visual Studio テンプレートやサンプルを使わず、最小限構成で「オウム返しボット」を開発してみます。
BotBuilder v4 は .NET Standard で開発されているため、.NET Framework および dotnet core のいずれでも動作しますが、クロスプラットフォームを意識して、dotnet core/Visual Studio Code (VSCode) で開発していきます。
事前準備
VS Code
C# 拡張機能
dotnet core 2.1
Bot Framework Emulator
プロジェクトの作成
ボットアプリとは、ただ単に Web API アプリケーションのことです。よって以下手順で Web API プロジェクトを作成します。
1. 以下コマンドを実行して Web API プロジェクトを作成。VSCode で開く。
dotnet new webapi-n myfirstbotcode myfirstbot2. 以下コマンドで開発時に使う証明書を信頼する。
dotnet dev-certs https--trust3. MVC Web API としてプロジェクトが作成されるが、標準の MVC 機能とコントローラーは使わないため、Contollers フォルダを削除。
4. Startup.cs より Configure および ConfigureServices の中身を以下コードに置き換え。MVC 機能を一切使わず、「Hello Bot!」という文字列だけを返す。
// This method gets called by the runtime. Use this method to add services to the container.publicvoidConfigureServices(IServiceCollectionservices){}// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.publicvoidConfigure(IApplicationBuilderapp,IHostingEnvironmentenv){app.Run(async(context)=>{awaitcontext.Response.WriteAsync("Hello, Bot!");});}5. F5 キーを押下してデバッグ実行。ドロップダウンより ".NET Core" を選択。F5 を再度押下してデバッグ開始。
Bot の追加
次に最低限必要な NuGet パッケージの追加とコードの追加を行います。
1. 以下コマンドで Microsoft.Bot.Builder.Integration.AspNet.Core NuGet パッケージを追加。
dotnet add package Microsoft.Bot.Builder.Integration.AspNet.Core2. ログより依存関係として Bot.Builder 関連のパッケージも追加されていることを確認。
3. プロジェクトに MyBot.cs ファイルを追加して、IBot インターフェースを継承。
4. 赤線が出ているところにカーソルを合わせて 「Ctrl ⁺ . (ドット)」 ショートカットを押下。選択肢より「using Microsoft.Bot.Builder;」を選択。自動的に using ステートメントが追加される。
5. 再度、赤線が出ているところにカーソルを合わせて「Ctrl ⁺ . (ドット)」 ショートカットを押下。選択肢より「Implement interface」を選択。必要なメソッドが追加される。
6. OnTurnAsync が追加されるので、async キーワードを追加。ボットが実行されると、この OnTurnAsync メソッドが実行される。
7. 以下のコードを追加。
awaitturnContext.SendActivityAsync("Hello from MyBot");8. 結果として MyBot.cs は以下の様になる。
usingSystem.Threading;usingSystem.Threading.Tasks;usingMicrosoft.Bot.Builder;publicclassMyBot:IBot{publicasyncTaskOnTurnAsync(ITurnContextturnContext,CancellationTokencancellationToken=default(CancellationToken)){awaitturnContext.SendActivityAsync("Hello from MyBot");}}9. 次に Start.cs を以下の様に変更。using の追加と ConfigureServices および Configure メソッドに Bot 関連のコードを追加。
usingMicrosoft.AspNetCore.Builder;usingMicrosoft.AspNetCore.Hosting;usingMicrosoft.Bot.Builder.Integration.AspNet.Core;usingMicrosoft.Extensions.Configuration;usingMicrosoft.Extensions.DependencyInjection;namespacemyfirstbot{publicclassStartup{publicStartup(IConfigurationconfiguration){Configuration=configuration;}publicIConfigurationConfiguration{get;}// This method gets called by the runtime. Use this method to add services to the container.publicvoidConfigureServices(IServiceCollectionservices){services.AddBot<MyBot>();}// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.publicvoidConfigure(IApplicationBuilderapp,IHostingEnvironmentenv){app.UseBotFramework();}}}Bot のテスト
では実際に現時点の動作を確認してみます。
1. F5 キーを押下して Bot を実行。コンソールより起動したポートを確認。http を使うため、この環境ではポート 5000。
2. Bot Framework Emulator を起動。「create new bot configuration」をクリック。
3. 名前に「MyBot」、Endpoint URL に「http://localhost:5000/api/messages」と入力。ポートは環境に合わせて変更するが、「/api/messages」を忘れない。
4. 「Save and connect」をクリック。ファイルの保存を聞かれるので、MyFirstBot フォルダに保存。
5. メッセージを打つ前に 2 つ「Hello from MyBot」が返ることを確認。
6. 手動でメッセージを送信。「Hello from MyBot」が返ることを確認。
7. デバッグを停止。
Bot の改修
現在は文字を打つ前に返信があるため、ユーザーのメッセージにだけ反応し、オウム返しをするように改修します。
1. MyBot.cs を以下のコードと差し替え。
usingSystem.Threading;usingSystem.Threading.Tasks;usingMicrosoft.Bot.Builder;usingMicrosoft.Bot.Schema;publicclassMyBot:IBot{publicasyncTaskOnTurnAsync(ITurnContextturnContext,CancellationTokencancellationToken=default(CancellationToken)){// ユーザーのメッセージにだけ返信// Text プロパティの値をオウム返しif(turnContext.Activity.Type==ActivityTypes.Message&&!string.IsNullOrEmpty(turnContext.Activity.Text))awaitturnContext.SendActivityAsync(turnContext.Activity.Text);}}2. F5 を押下して起動。エミュレータで「Start Over」をクリック。
まとめ
今回は、まず最小構成で「オウム返しボット」を開発しました。重要な事は以下の 3 点です。
- ボットは IBot インターフェースを継承する
- OnTurnAsync に実際のコードを書く
- Startup.cs に BotFramework 呼び出しのコードを書く
次回は、今回のコードが実際にどういう仕組みで動作しているのかを見ていきます。
Register as a new user and use Qiita more conveniently
- You get articles that match your needs
- You can efficiently read back useful information
- You can use dark theme


