LoginSignup
0
0

Rinnaさんとおしゃべりアプリ

Last updated at Posted at 2022-10-30

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を使うと簡単にテストできる。

スクリーンショット 2022-10-30 22.19.29.png

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0