C# 开发AliYun(阿里云) 小蜜调用接口代码

using System;
using System.Collections.Generic;
using Aliyun.Acs.Core;
using Aliyun.Acs.Core.Exceptions;
using Aliyun.Acs.Core.Profile;
using Aliyun.Acs.Chatbot.Model.V20171011;
using Newtonsoft.Json;

namespace ChatbotDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            while (true)
            {
                string txt = Console.ReadLine();
                string msg = null;

            IClientProfile profile = DefaultProfile.GetProfile("cn-shanghai", "<accessKeyId>", "<accessSecret>");
            DefaultAcsClient client = new DefaultAcsClient(profile);

                var request = new ChatRequest();
                //request.SenderNick = "1";
                //request.SenderId = "3";
                //request.KnowledgeId = "2";
                //request.SessionId = "1";
                request.Utterance = txt;
                request.InstanceId = "chatbot-cn-mp913ow5";//你的机器人id
                try
                {
                    var response = client.GetAcsResponse(request);
                    if (response == null)
                    {
                        msg = "请求失败:response不能为空";
                    }
                    else
                    {
                        msg = $"小蜜:{response.Messages[0].Text.Content}" +
                            $"{System.Environment.NewLine}";
                    }

                }
                catch (ServerException e)
                {
                    msg = $"ServerException:{e}";
                }
                catch (ClientException e)
                {
                    msg = $"ClientException:{e}";
                }

                Console.WriteLine(msg);
            }

        }
    }
}

 

posted @ 2019-04-29 00:33  少时不知贵  阅读(1835)  评论(0编辑  收藏  举报