【发布时间】:2019-04-22 07:24:11
【问题描述】:
我正在尝试从我的 Azure 托管机器人主动向 Microsoft Teams 团队聊天发送临时消息。我已成功向用户与机器人进行的一对一聊天发送消息。
我已经看到 this documentation 详细说明了如何执行此操作,但我相信这是 Bot Framework 版本 3。它使用块块包 Microsoft.Bot.Connector.Teams.Models,版本 4 不再支持它。我无法找到执行此操作的文档。
为了更具描述性:
我有一个机器人,它从我的网络应用程序接收带有警报数据的 POST 请求。当机器人收到其中一个 POST 请求时,它会将消息转发到团队聊天。此消息将在任何上下文之外发送。可以在此处找到我用于转发到 1 对 1 聊天的代码。
/// <summary>
/// Forwards a message to a personal chat.
/// The details of who to send the message to is included in the <paramref name="forwardContext"/>.
/// </summary>
/// <param name="forwardContext">JSON of the recipient details.</param>
/// <param name="messageToSend">Text message to send to chat.</param>
/// <returns>A <see cref="Task"/> representing the asynchronous messsage forward.</returns>
private async Task ForwardMessageToPersonalChatAsync(JToken forwardContext, string messageToSend)
{
// Collect data from JSON input
var restCmd = forwardContext;
var toId = (string)restCmd["toId"];
var toName = (string)restCmd["toName"];
var fromId = (string)restCmd["fromId"];
var fromName = (string)restCmd["fromName"];
var channelId = (string)restCmd["channel"];
var serviceURL = (string)restCmd["serviceURL"];
var conversationId = (string)restCmd["conversation"];
var tenant = (string)restCmd["tenant"];
var cred_str = $@"toId: {toId}
toName: {toName}
fromId: {fromId}
fromName: {fromName}
channelId: {channelId}
serviceURL: {serviceURL}
conversationId: {conversationId}";
this.logger.LogInformation(cred_str);
this.logger.LogInformation($"Forwarding the following message to {toName}: {messageToSend}");
var uri = new System.Uri(serviceURL);
Dictionary<string, string> botCreds = this.GetBotCredentials();
ConnectorClient connector = new ConnectorClient(uri, botCreds["App ID"], botCreds["App Password"]);
var activity = new Activity
{
Type = ActivityTypes.Message,
From = new ChannelAccount(fromId, fromName),
Recipient = new ChannelAccount(toId, toName),
Conversation = new ConversationAccount(false, "personal", conversationId),
Text = messageToSend,
};
try
{
MicrosoftAppCredentials.TrustServiceUrl(serviceURL);
await connector.Conversations.SendToConversationAsync(conversationId, activity);
}
catch (System.Exception ex)
{
this.logger.LogError(ex.ToString());
}
}
如何向团队聊天发送主动消息?
感谢您的帮助。
【问题讨论】:
标签: c# botframework microsoft-teams