【问题标题】:Microsoft Bot Framework: broadcast messages to users from databaseMicrosoft Bot Framework:从数据库向用户广播消息
【发布时间】:2017-07-04 00:31:25
【问题描述】:

情况:当用户注册机器人时(通过向机器人输入消息,我将该用户的信息存储在数据库中: - 用户身份 - 用户名, - 服务网址

在某个时间点,我想让我的机器人向该表中的所有用户广播一条消息。

foreach (var bUsers in users)
{
MicrosoftAppCredentials.TrustServiceUrl(bUsers.ServiceUrl);
                        MicrosoftAppCredentials creds = new MicrosoftAppCredentials("<<appid>>", "<<secret>>");
                        var connector = new ConnectorClient(new Uri(bUsers.ServiceUrl), creds);
                        var conversationId = await connector.Conversations.CreateDirectConversationAsync(new ChannelAccount(), new ChannelAccount(bUsers.UserId, bUsers.UserName));
                        message = Activity.CreateMessageActivity();
                        message.From = botAccount;
                        message.Recipient = userAccount;
                        message.Conversation = new ConversationAccount(id: conversationId.Id);
                        message.Text = "Hello from " + context.Activity.From.Name;
                        message.Locale = "en-Us";
                        var reply = await connector.Conversations.SendToConversationAsync((Activity) message);
}

使用此代码,我收到一条消息:

teamChannelId 中的对话 ID 无效

我不明白这个消息,甚至可以做我想做的事吗?

【问题讨论】:

    标签: c# frameworks bots


    【解决方案1】:

    我在做几乎和你一样的事情,但它突然停止工作了。我可以看到相同的错误消息。

    但就我而言,这是错误的,也许它一开始就奏效了,这很奇怪。因为我使用了client.UserId,它被设置为activity.Conversation.Id。如果我更改代码以将其用作conversationId,它就可以工作。

    这是我现在正在运行的代码,旧部分已被注释掉:

        public static async Task SendMessageToClient(ServerClient client, string messageText)
        {
            var connector = new ConnectorClient(new Uri(client.BotServiceUrl), new MicrosoftAppCredentials());
            var userAccount = new ChannelAccount(name: client.UserName, id: client.UserId);
            var botAccount = new ChannelAccount(name: client.BotName, id: client.BotId);
            // this worked before but not anymore
            //var conversationId = await connector.Conversations
            //       .CreateDirectConversationAsync(botAccount, userAccount).Id;
    
            // because client.UserId was set in a MessageController to activity.Conversation.Id we can use this
            var conversationId = client.UserId; 
            var message = Activity.CreateMessageActivity();
            message.From = botAccount;
            message.Recipient = userAccount;
            message.Conversation = new ConversationAccount(false, conversationId);
            message.Locale = "en-Us";
            if (client.ReplaceFrom != null && client.ReplaceTo != null)
            {
                messageText = messageText.Replace(client.ReplaceFrom, client.ReplaceTo);
            }
            message.Text = messageText;
            await connector.Conversations.SendToConversationAsync((Activity) message);
        }
    

    【讨论】:

    • 在我的情况下,使用模拟器,机器人什么也没说。它清除聊天窗口。奇怪的是它曾经工作到两天前。我设法同时向一些 Skype 用户发送了一条消息。
    • 供将来使用...github.com/Microsoft/BotBuilder/issues/2294 似乎与更新到 3.5.3 相关
    猜你喜欢
    • 1970-01-01
    • 2017-04-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-02
    • 2016-09-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多