C# Microsoft Bot框架:从数据库向用户广播消息
情况:当用户注册bot时(通过向bot输入消息,我将该用户的信息存储在数据库中: -用户ID -用户名, -服务URL 在某个时间点,我想让我的机器人向该表中的所有用户广播一条消息C# Microsoft Bot框架:从数据库向用户广播消息,c#,frameworks,bots,C#,Frameworks,Bots,情况:当用户注册bot时(通过向bot输入消息,我将该用户的信息存储在数据库中: -用户ID -用户名, -服务URL 在某个时间点,我想让我的机器人向该表中的所有用户广播一条消息 foreach (var bUsers in users) { MicrosoftAppCredentials.TrustServiceUrl(bUsers.ServiceUrl); MicrosoftAppCredentials creds = new Microso
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);
}
foreach(用户中的var总线)
{
MicrosoftAppCredentials.TrustServiceUrl(bUsers.ServiceUrl);
MicrosoftAppCredentials creds=新的MicrosoftAppCredentials(“,”);
var connector=newconnectorclient(新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=新的ConversationAccount(id:conversationId.id);
message.Text=“Hello from”+context.Activity.from.Name;
message.Locale=“en-Us”;
var reply=wait connector.Conversations.SendToConversationAsync((活动)消息);
}
有了这段代码,我会收到一条消息说:
teamsChannelId中的对话ID无效
我不明白这条消息,甚至可以做我想做的事情吗?我做的几乎和你做的一样,但它突然停止工作。我可以看到相同的错误消息 但在我的例子中,它只是错误的,可能一开始就很奇怪。因为我使用了
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用户发送了一条消息。Fot未来使用…似乎与3.5.3的更新有关