Aida-chiyo-Talk-app

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

TalkEngine.cs (3476B)


      1 using System;
      2 using System.Collections;
      3 using System.Collections.Generic;
      4 using System.Diagnostics;
      5 using System.Linq;
      6 using System.Net.Http;
      7 using System.Net;
      8 using System.Net.Http.Headers;
      9 using System.Net.Http.Json;
     10 using System.Text;
     11 using System.Text.Json;
     12 using System.Text.Json.Serialization;
     13 using System.Threading.Tasks;
     14 using System.Xml;
     15 using static Aida_chiyo_Talk_app.TalkEngine;
     16 using System.Runtime.CompilerServices;
     17 
     18 namespace Aida_chiyo_Talk_app
     19 {
     20     internal class TalkEngine
     21     {
     22         string answer = string.Empty;
     23         /*ChatGPT
     24         string APIkey = "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";
     25         */
     26 
     27         /*A3rt系統
     28         string APIurl = "https://api.a3rt.recruit.co.jp/talk/v1/smalltalk";
     29         string talkAPIkey = "DZZqpz3HVnJL4LGk57u8ZfmgfAIjrB3B";
     30         */
     31 
     32         //mibo
     33         string APIurl = "https://api-mebo.dev/api";
     34         string talkAPIkey = "7626250d-bd09-45c4-ac83-a54848e692ba189a96ad2df2a8";
     35         string AgentID = "2a49a388-d706-4116-8f0b-6a7a660f667b189a9680a98e9";
     36 
     37         public string CallTalkAPI(string input)
     38         {
     39             //文字列をUTF-8にエンコード
     40             byte[] bytesData = System.Text.Encoding.UTF8.GetBytes(input);
     41             Encoding utf8Enc = Encoding.GetEncoding("UTF-8");
     42             string utf = utf8Enc.GetString(bytesData);
     43 
     44             Console.WriteLine("We are calling API");
     45 
     46             //API呼び出し
     47             var task = talkAPI(talkAPIkey,APIurl,utf);
     48             while (task.IsCompletedSuccessfully == false)
     49             {
     50                 
     51             }
     52             answer = task.ToString();
     53             return answer;
     54         }
     55         public async Task talkAPI(string apikey, string apiurl, string input)
     56         {
     57             // Web API を呼ぶための HttpClient を作る
     58             var client = new HttpClient();
     59             // POST メソッドで JSON の Body のリクエストを投げる
     60             var response = await client.PostAsJsonAsync(
     61                 apiurl,
     62                 //new RequestBody { apikey = apikey , query = input} );
     63                 new RequestBody { api_key = apikey, agent_id = AgentID, utterance = input });
     64             // レスポンスのステータスコードが成功していたら Answer の値を出力
     65             if (response.IsSuccessStatusCode)
     66             {
     67                 var responseBody = await response.Content.ReadFromJsonAsync<Response>();
     68                 Console.WriteLine(responseBody?.utterance);
     69             }
     70             else
     71             {
     72                 Console.WriteLine(response.StatusCode);
     73             }
     74         }
     75 
     76     }
     77 
     78     class RequestBody
     79     {
     80         public string? api_key { get; set; }
     81         public string? agent_id { get; set; }
     82         public string? utterance { get; set; }
     83     }
     84 
     85     class Response
     86     {
     87         public string? utterance { get; set; }
     88         public string? options { get;set; }
     89         public string? topic {  get; set; }
     90         public string? imageUrl { get; set; }
     91         public string? url { get; set; }
     92         public string? isAutoResponse { get; set; }
     93 
     94     }
     95     /*A3rt系統
     96     class RequestBody
     97     {
     98         public string? apikey { get; set; }
     99         public string? callback { get; set; }
    100         public string? query { get; set; }
    101     }
    102 
    103     class Response
    104     {
    105         public string? status { get; set; }
    106         public string? message { get; set; }
    107         public string? results { get; set; }
    108     }
    109     */
    110 }