【问题标题】:Send message to bot from web service从 Web 服务向机器人发送消息
【发布时间】:2018-06-27 11:17:42
【问题描述】:

我正在从我的 .NET 机器人启动一个网页。该页面通过我们的一个后端系统与用户进行交互。交互结束后,我需要向机器人发送状态更新消息 - 它位于上下文中。在等待该消息的同时等待。

目前该机器人正在使用 Facebook 频道并通过 Facebook 网址按钮启动页面,但最终它需要跨多个频道工作。

从网站上,我可以轻松地向用户发送消息,但尽管花费了数小时搜索和尝试不同的机制,我还没有找到向机器人发送消息的方法。

基于https://docs.botframework.com/en-us/csharp/builder/sdkreference/d1/df2/_conversation_reference_ex_8cs_source.html 的最新尝试,(cr 已缓存对话详细信息):

string MicrosoftAppId = ConfigurationManager.AppSettings["MicrosoftAppId"];
            string MicrosoftAppPassword = ConfigurationManager.AppSettings["MicrosoftAppPassword"];

            var account = new MicrosoftAppCredentials(MicrosoftAppId, MicrosoftAppPassword);
            MicrosoftAppCredentials.TrustServiceUrl(cr.serviceUrl);

            var connector = new ConnectorClient(new Uri(cr.serviceUrl), account);

            Activity activity = new Activity
            {
                Type = ActivityTypes.Message,
                Id = Guid.NewGuid().ToString(),
                Recipient = new ChannelAccount
                {
                    Id = cr.bot.id,
                    Name = cr.bot.name
                },
                ChannelId = cr.channelId,
                ServiceUrl = cr.serviceUrl,
                Conversation = new ConversationAccount
                {
                    Id = cr.conversation.id,
                    IsGroup = false,
                    Name = null
                },
                From = new ChannelAccount
                {
                    Id = cr.bot.id,
                    Name = cr.bot.name
                },
                Text = "Test send message to bot from web service"
            };

            try
            {
                await connector.Conversations.SendToConversationAsync(activity);
            }
            catch (Exception ex)
            {
                var s = ex.Message;
            }

但似乎没有从/收件人的组合发送到机器人。

我确定我错过了一些简单的东西,你们可以告诉我它是什么!

【问题讨论】:

    标签: c# botframework


    【解决方案1】:

    以下是从另一个应用程序向机器人发送消息的示例。在这种情况下,我是从一个 Web API 执行此操作的,该 API 是一个代理,拦截来自用户的消息并将它们发送到机器人。这段代码中不包括如何构建一个活动,但看起来你已经对我们进行了排序。请注意,在这个辅助应用程序中,我使用了Bot.Builder,因此我可以使用活动对象和其他功能。

    //get a token (See below)
    var token = GetToken();
    
    //set the service url where you want this activity to be replied to
    activity.ServiceUrl = "http://localhost:4643/api/return";
    
    //convert an activity to json to send to bot
    var jsonActivityAltered = JsonConvert.SerializeObject(activity);
    
    //send a Web Request to the bot
    using (var client = new WebClient())
    {
        //add your headers
        client.Headers.Add("Content-Type", "application/json");
        client.Headers.Add("Authorization", $"Bearer {token}");
    
        try
        {
            //set where to to send the request {Your Bots Endpoint}
            var btmResponse = client.UploadString("http://localhost:3971/api/messages", jsonActivityAltered);
        }
        catch (Exception e)
        {
            Console.WriteLine(e);
            throw;
        }
    }
    

    获取令牌:

    private static string GetToken()
    {
        string token;
        using (var client = new WebClient())
        {
            var values = new NameValueCollection();
            values["grant_type"] = "client_credentials";
            values["client_id"] = "{MS APP ID}";
            values["client_secret"] = "{MS APP SECRET}";
            values["scope"] = "{MS APP ID}/.default";
    
            var response =
                client.UploadValues("https://login.microsoftonline.com/botframework.com/oauth2/v2.0/token", values);
    
            var responseString = Encoding.Default.GetString(response);
            var result = JsonConvert.DeserializeObject<ResponseObject>(responseString);
            token = result.access_token;
        }
    
        return token;
    }
    

    响应对象类:

    public class ResponseObject
    {
        public string token_type { get; set; }
        public int expires_in { get; set; }
        public int ext_expires_in { get; set; }
        public string access_token { get; set; }
    }
    

    【讨论】:

      猜你喜欢
      • 2019-06-27
      • 2012-09-21
      • 2018-06-29
      • 1970-01-01
      • 2018-05-17
      • 2018-07-27
      • 1970-01-01
      • 2021-03-19
      • 2021-01-28
      相关资源
      最近更新 更多