Rinna Developerで公開されてるAPIを試してみた。
上のサイトを参考にチャットbotを作成します。
dotnet new echobot -n <your-bot-name>
作成したテンプレートのEchoBot.cs
を下のように書き換えます。
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
//
// Generated with EchoBot .NET Template version v4.17.1
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Bot.Builder;
using Microsoft.Bot.Schema;
using Newtonsoft.Json.Linq;
namespace EchoBot.Bots
{
public class EchoBot : ActivityHandler
{
protected override async Task OnMessageActivityAsync(ITurnContext<IMessageActivity> turnContext, CancellationToken cancellationToken)
{
var content = new StringContent("{ \"rawInput\":\"" + turnContext.Activity.Text + "\", \"outputLength\": 25 }");
var client = new HttpClient();
// Request headers
client.DefaultRequestHeaders.CacheControl = CacheControlHeaderValue.Parse("no-cache");
client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", "******");
var uri = "https://api.rinna.co.jp/models/cce";
HttpResponseMessage response;
// Request body
content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
response = await client.PostAsync(uri, content);
Console.Write(response);
JObject json = JObject.Parse(response.Content.ReadAsStringAsync().Result);
var replyText = $"Echo: {json["answer"]}";
await turnContext.SendActivityAsync(MessageFactory.Text(replyText, replyText), cancellationToken);
}
}
}
Bot Emulatorを使うと簡単にテストできる。