gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 15:08:28 26 4
gpt4 key购买 nike

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

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

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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com