试图推动使用bot框架

时间:2017-06-02 16:59:28

标签: c# botframework

开始使用bot框架并尝试向用户“推送”消息。我使用SendToConversationAsync复制了上一篇文章中的一些代码,并从模拟器中粘贴了ids等,但它没有发送。我已粘贴下面的代码。任何帮助表示赞赏。

namespace Bot
{
    [BotAuthentication]
    public class MessagesController : ApiController
    {    
        public async Task<HttpResponseMessage> Post([FromBody]Activity activity)
        {
            if (activity.Type == ActivityTypes.Message)
            {
                ConnectorClient connector = new ConnectorClient(new Uri(activity.ServiceUrl));
                // calculate something for us to return
                int length = (activity.Text ?? string.Empty).Length;

                //create reply here

                // return our reply to the user
                Activity reply = activity.CreateReply($"You sent **{activity.Text}** which was {length} characters");
                await connector.Conversations.ReplyToActivityAsync(reply);

                SendMessage("Blah!");
            }
            else
            {
                HandleSystemMessage(activity);
            }
            var response = Request.CreateResponse(HttpStatusCode.OK);
            return response;
        }    

        private void SendMessage(string msg)
        {
            var userAccount = new ChannelAccount(name: "", id: "default-user");
            var botAccount = new ChannelAccount(name: "Bot", id: "m374hm6j2b474lei9");
            var connector = new ConnectorClient(new Uri("http://localhost:3979/"));

            // conversationId for Real Bot
            //var conversationId = connector.Conversations.CreateDirectConversationAsync(botAccount, userAccount).Result;

    // conversationId for Bot Emulator
    //var conversationId = _activityHistory.Conversation;

            IMessageActivity message = Activity.CreateMessageActivity();
            message.From = botAccount;
            message.Recipient = userAccount;
            message.Conversation = new ConversationAccount(id: "0dkmb2e3lf1dn2g69");
            message.Text = msg;
            message.Locale = "en-Us";
            connector.Conversations.SendToConversationAsync((Activity)message);
        }
    }
}

更多信息:

JSON

{
  "type": "message",
  "text": "test",
  "from": {
    "id": "default-user",
    "name": "User"
  },
  "locale": "en-US",
  "textFormat": "plain",
  "timestamp": "2017-06-02T21:33:24.737Z",
  "channelData": {
    "clientActivityId": "1496437792941.9667812978932935.10"
  },
  "id": "hfncb55l7j7ea0c26",
  "channelId": "emulator",
  "localTimestamp": "2017-06-02T14:33:24-07:00",
  "recipient": {
    "id": "m374hm6j2b474lei9",
    "name": "Bot"
  },
  "conversation": {
    "id": "icn5ed433edc0kh99"
  },
  "serviceUrl": "http://127.0.0.1:56303"
}

的SendMessage:

private void SendMessage(string msg)
{
    var userAccount = new ChannelAccount(name: "User", id: "default-user");
    var botAccount = new ChannelAccount(name: "Bot", id: "m374hm6j2b474lei9");
    var connector = new ConnectorClient(new Uri("http://localhost:3979/"));

    // conversationId for Real Bot
    //var conversationId = connector.Conversations.CreateDirectConversationAsync(botAccount, userAccount).Result;

    // conversationId for Bot Emulator
    //var conversationId = _activityHistory.Conversation;

    IMessageActivity message = Activity.CreateMessageActivity();
    message.From = botAccount;
    message.Recipient = userAccount;
    message.Conversation = new ConversationAccount(id: "icn5ed433edc0kh99");
    message.Text = msg;
    message.Locale = "en-Us";
    //message.Type = "message";
    //message.ChannelId = "emulator";
    //message.ReplyToId = "62ehe9jk6kn2eg23i";
    message.ServiceUrl = "http://127.0.0.1:56303";

    connector.Conversations.SendToConversationAsync((Activity)message);
}

2 个答案:

答案 0 :(得分:0)

http://localhost:3979/不是有效的服务网址。这是托管机器人的URL,而不是您用于从机器人与用户通信的URL。

请查看模拟器中对话更新事件的POST。

[13:05:10] -> POST 200 [conversationUpdate] 

在“详细信息”窗格中,您将看到底部附近的serviceUrl。这是模拟器正在侦听的URL,以及用于从机器人与用户通信的URL。应该从先前的活动缓存ServiceUrl。

enter image description here

答案 1 :(得分:0)

谢谢大家。我想我会选择使用Microsoft.Bot.Builder.Dialogs。他们在github上有一些例子,可以在计时器上运行,并且可以为我工作。 BotBuilder-Samples