c# - 微软机器人框架 : broadcast messages to users from database

标签 c# frameworks bots

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

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

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);
}

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

Invalid conversation ID in teamsChannelId

我不明白这条信息,是否可以按照我的意愿行事?

最佳答案

我正在做与您几乎相同的事情,但它突然停止工作。我可以看到相同的错误消息。

但在我的例子中,这是错误的,也许它一开始就起作用很奇怪。因为我使用了设置为 activity.Conversation.Idclient.UserId。如果我更改代码以将其用作 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);
    }

关于c# - 微软机器人框架 : broadcast messages to users from database,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42247819/

相关文章:

c# - Entity Framework 与有时独立的类一对一

c# - 在 C# 中没有等效项的 Java 语言功能

php - 如果我学习 Codeigniter,我可以将它与 Drupal 一起使用吗?或者这是一个坏主意,我应该使用 Expression Engine?

python - 对此 Discord.py 重写 + react 灯代码感到困惑 - 需要解释

javascript - 当有人在 Discord.js 中输入未知命令时,如何发出 "Invalid Command"消息?

bots - 如何结束机器人对话并让真人处理 Dialogflow 中的响应?

c# - 我应该如何确定 MSAL 帐户是否具有基于 Exchange 的电子邮件系统? (有一个异常(exception))

c# - MVC 4 后期绑定(bind) DataContext 实体 LINQ 引用

java - 将多个注释与参数合并

objective-c - 如何在 Xcode 中保护我的框架?