Bots 针对机器人新手的带直连Rest API的Microsoft机器人

Bots 针对机器人新手的带直连Rest API的Microsoft机器人,bots,Bots,我是微软机器人的新手。 我的公司有自己的通讯应用程序,我想把我的机器人和通讯应用程序连接起来,我的客户可以在我的公司通讯应用程序上使用我的机器人。我读到它需要使用直接线做这件事。但我真的不知道怎么做。 有人帮我吗?或者给我一些建议?或者任何这样的例子。 非常感谢。请参考有关Bot框架的直接线路方法 您需要做的是将其用作端点,并调用文档中所示的API 示例:-我刚刚尝试使用ASP.MVC应用程序。我创建了一个文本框和按钮,用于向bot提交消息 首先在bot应用程序中启用直接链接。那就记住这个秘密

我是微软机器人的新手。 我的公司有自己的通讯应用程序,我想把我的机器人和通讯应用程序连接起来,我的客户可以在我的公司通讯应用程序上使用我的机器人。我读到它需要使用直接线做这件事。但我真的不知道怎么做。 有人帮我吗?或者给我一些建议?或者任何这样的例子。 非常感谢。

请参考有关Bot框架的直接线路方法

您需要做的是将其用作端点,并调用文档中所示的API

示例:-我刚刚尝试使用ASP.MVC应用程序。我创建了一个文本框和按钮,用于向bot提交消息

  • 首先在bot应用程序中启用直接链接。那就记住这个秘密
  • 下面的代码示例演示如何将聊天应用程序或公司应用程序连接到使用bot框架构建的bot

  • 首先,您需要授权您访问direct link API

    client = new HttpClient();
    client.BaseAddress = new Uri("https://directline.botframework.com/api/conversations/");
    client.DefaultRequestHeaders.Accept.Clear();
    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
    client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("BotConnector", "[Your Secret Key Here]");
    
    response=wait client.GetAsync(“/api/tokens/”)

    if(响应。IsSuccessStatusCode)

  • 如果之前的回复成功,您可以开始新的对话 模型-

    公开课对话 { 公共字符串会话ID{get;set;} 公共字符串标记{get;set;} 公共字符串eTag{get;set;} }

  • 控制器内部代码-

     var conversation = new Conversation();
     response = await client.PostAsJsonAsync("/api/conversations/",conversation);
     if (response.IsSuccessStatusCode)
    
    如果此响应成功,您将获得conversationId和启动消息传递的令牌

  • 然后通过以下代码将消息传递给bot

    Conversation ConversationInfo=response.Content.ReadAsAsync(类型(对话))。结果为对话; 字符串conversationUrl=ConversationInfo.conversationId+“/messages/”; Message msg=new Message(){text=Message}; response=wait client.postsjsonasync(conversationUrl,msg); if(响应。IsSuccessStatusCode)

  • 如果您得到成功响应,则表示您已将消息发送到bot。现在您需要从BOT获取回复消息

  • 要从bot获取消息

    response=wait client.GetAsync(conversationUrl); if(响应。IsSuccessStatusCode){ MessageSet BotMessage=response.Content.ReadAsAsync(typeof(MessageSet))。结果为MessageSet; ViewBag.Messages=BotMessage; IsReplyReceived=真; }

  • 在这里,您将获得一个消息集,这意味着您发送的消息和来自Bot的回复。您现在可以在聊天窗口中显示它

    消息模型-

     public class MessageSet
    {
        public Message[] messages { get; set; }
        public string watermark { get; set; }
        public string eTag { get; set; }
    }
    
    public class Message
    {
        public string id { get; set; }
        public string conversationId { get; set; }
        public DateTime created { get; set; }
        public string from { get; set; }
        public string text { get; set; }
        public string channelData { get; set; }
        public string[] images { get; set; }
        public Attachment[] attachments { get; set; }
        public string eTag { get; set; }
    }
    
    public class Attachment
    {
        public string url { get; set; }
        public string contentType { get; set; }
    }
    
    使用这些API调用,您可以轻松地将任何自定义聊天应用程序与bot框架连接起来。下面是一个方法中的完整代码,让您了解如何归档您的目标

     private async Task<bool> PostMessage(string message)
        {
            bool IsReplyReceived = false;
    
            client = new HttpClient();
            client.BaseAddress = new Uri("https://directline.botframework.com/api/conversations/");
            client.DefaultRequestHeaders.Accept.Clear();
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("BotConnector", "[Your Secret Code Here]");
            response = await client.GetAsync("/api/tokens/");
            if (response.IsSuccessStatusCode)
            {
                var conversation = new Conversation();
                response = await client.PostAsJsonAsync("/api/conversations/", conversation);
                if (response.IsSuccessStatusCode)
                {
                    Conversation ConversationInfo = response.Content.ReadAsAsync(typeof(Conversation)).Result as Conversation;
                    string conversationUrl = ConversationInfo.conversationId+"/messages/";
                    Message msg = new Message() { text = message };
                    response = await client.PostAsJsonAsync(conversationUrl,msg);
                    if (response.IsSuccessStatusCode)
                    {
                        response = await client.GetAsync(conversationUrl);
                        if (response.IsSuccessStatusCode)
                        {
                            MessageSet BotMessage = response.Content.ReadAsAsync(typeof(MessageSet)).Result as MessageSet;
                            ViewBag.Messages = BotMessage;
                            IsReplyReceived = true;
                        }
                    }
                }
    
            }
            return IsReplyReceived;
        }
    
    private async Task PostMessage(字符串消息)
    {
    bool IsReplyReceived=false;
    client=新的HttpClient();
    client.BaseAddress=新Uri(“https://directline.botframework.com/api/conversations/");
    client.DefaultRequestHeaders.Accept.Clear();
    client.DefaultRequestHeaders.Accept.Add(新的MediaTypeWithQualityHeaderValue(“应用程序/json”);
    client.DefaultRequestHeaders.Authorization=新的AuthenticationHeaderValue(“BotConnector”,“此处的[您的密码]”);
    response=wait client.GetAsync(“/api/tokens/”);
    if(响应。IsSuccessStatusCode)
    {
    var conversation=newconversation();
    response=wait client.PostAsJsonAsync(“/api/conversations/”,conversation);
    if(响应。IsSuccessStatusCode)
    {
    Conversation ConversationInfo=response.Content.ReadAsAsync(类型(对话))。结果为对话;
    字符串conversationUrl=ConversationInfo.conversationId+“/messages/”;
    Message msg=new Message(){text=Message};
    response=wait client.postsjsonasync(conversationUrl,msg);
    if(响应。IsSuccessStatusCode)
    {
    response=wait client.GetAsync(conversationUrl);
    if(响应。IsSuccessStatusCode)
    {
    MessageSet BotMessage=response.Content.ReadAsAsync(typeof(MessageSet))。结果为MessageSet;
    ViewBag.Messages=BotMessage;
    IsReplyReceived=真;
    }
    }
    }
    }
    已收到退货;
    }
    

    感谢您的机器人加油。

    您现在可以使用DirectLine SDK:)