使用网络应用程序通过机器人主动发送消息
Proactively sending message through bot using a web app
已解决
感谢用户 MarkMoose,我意识到我的数据库 table 没有存储完整的 ID。
未来的调试人员:如果这不能帮助您解决问题,请查看我与 MarkMoose 的对话,他们向我介绍了非常有用的故障排除步骤。
我正在尝试使用 Microsoft Bot SDK 版本 4.0 创建托管在 Azure 上的 Microsoft Teams 机器人。
流程如下
Web 应用警报触发器。向机器人发送 POST 请求,其中包含(从用户之前的消息中收集的所有这些数据)
- 至(收件人用户的)ID
- 收件人姓名(收件人用户)
- 来自 ID(我的机器人)
- 发件人姓名(我的机器人)
- 频道编号
- 对话 ID
- 服务 URL(收件人用户)
Bot 从 JSON 中提取信息形成新消息 activity
- Bot 发送 activity 给用户
问题: 当 bot 尝试使用上面列出的凭据创建 ConversationAccount
对象时,它会抛出以下错误:
Exception caught: Microsoft.Bot.Schema.ErrorResponseException: Operation returned an invalid status code 'BadRequest'
这里是相关部分的代码。
请特别注意以下几行:
var account = new MicrosoftAppCredentials(botCreds["App ID"], botCreds["App Password"]);
var jwtToken = await account.GetTokenAsync();
ConnectorClient connector = new ConnectorClient(new System.Uri(serviceURL), account);
当我找到其他人对我的问题的解决方案时,这些行略有不同。在我当前使用的代码版本中 。我也试过他创建的 DelegatingHandler class 但它抛出同样的错误。
/// <summary>
/// Sends a message to a user or group chat.
/// </summary>
/// <param name="forwardContext">JSON object containing credentials for destination chat.</param>
/// <param name="messageToSend">The message to forward.</param>
/// <returns></returns>
private async Task ForwardMessage(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 cred_str = $@"toId: {toId}
toName: {toName}
fromId: {fromId}
fromName: {fromName}
channelId: {channelId}
serviceURL: {serviceURL}
conversationId: {conversationId}";
_logger.LogInformation(cred_str);
_logger.LogInformation($"Forwarding the following message to {toName}: {messageToSend}");
Dictionary<string, string> botCreds = GetBotCredentials();
// Create relevant accounts
ChannelAccount userAccount = new ChannelAccount(name: toName, id: toId);
ChannelAccount botAccount = new ChannelAccount(name: fromName, id: fromId);
if (!MicrosoftAppCredentials.IsTrustedServiceUrl(serviceURL))
{
_logger.LogInformation($"Adding to trusted service urls: {serviceURL}");
// Register the service URL as trusted
MicrosoftAppCredentials.TrustServiceUrl(serviceURL);
}
MicrosoftAppCredentials.TrustServiceUrl(serviceURL);
var account = new MicrosoftAppCredentials(botCreds["App ID"], botCreds["App Password"]);
var jwtToken = await account.GetTokenAsync();
ConnectorClient connector = new ConnectorClient(new System.Uri(serviceURL), account);
// Create a new message activity
IMessageActivity message = Activity.CreateMessageActivity();
conversationId = (
await connector
.Conversations
.CreateDirectConversationAsync(botAccount, userAccount)).Id;
// Set relevant message details
message.From = botAccount;
message.Recipient = userAccount;
message.Text = messageToSend;
message.Locale = "en-Us";
message.ChannelId = channelId;
// Create a new converstaion and add it to the message.
message.Conversation = new ConversationAccount(id: conversationId);
await connector.Conversations.SendToConversationAsync((Activity) message);
}
这是我收集上述信息的代码。当用户首次与机器人交互时,将调用此函数。
/// <summary>
/// Called only when the !setup command is sent to the bot.
/// Updates the chats info in the DB.
/// </summary>
/// <param name="activity">Activity of the message the "!setup" command was sent in.</param>
/// <returns>True if the update query executed fine.</returns>
private bool SetupCommand(Activity activity)
{
// Connect to the database
this.database = new DBConnection(serverIP, databaseName, userName, password, _logger);
this.database.Connect();
var tableName = "ms_teams_chats";
// Data gathered from Activity for database.
// User ID
string toId = activity.From.Id;
// User Name
string toName = activity.From.Name;
// Bot ID
string fromId = activity.Recipient.Id;
// Bot Name
string fromName = activity.Recipient.Name;
// Users service URL
string serviceURL = activity.ServiceUrl;
// The platform the message came from. Example: 'skype'
string channelId = activity.ChannelId;
string conversationID = activity.Conversation.Id;
string conversationName = activity.Conversation.Name;
bool isGroupChat = activity.Conversation.IsGroup ?? false;
string upsertQuery = string.Empty;
upsertQuery = $@"
INSERT INTO {tableName}
(user_id, user_name, assoc_bot_id, assoc_bot_name, service_url, channel_id, conversation_id, is_group_chat)
VALUES (
'{toId}', '{toName}', '{fromId}', '{fromName}', '{serviceURL}', '{channelId}', '{conversationID}', {isGroupChat}
)
ON DUPLICATE KEY UPDATE
user_id = '{toId}',
user_name = '{toName}',
assoc_bot_id = '{fromId}',
assoc_bot_name = '{fromName}',
service_url = '{serviceURL}',
channel_id = '{channelId}',
conversation_id = '{conversationID}',
is_group_chat = {isGroupChat}
";
try
{
this.database.ExecuteNonQuery(upsertQuery);
}
catch (System.Exception e)
{
_logger.LogError($"Could not update users information. \nError:{e.ToString()}");
return false;
}
return true;
}
您似乎正在为我上周遇到的同样问题而苦苦挣扎。似乎 CreateDirectConversationAsync 在 MS Teams 中不起作用,因为 Teams 也需要一个 tennantId。
我在这里找到了关于此的声明:https://github.com/Microsoft/BotBuilder/issues/2944
答案提到了一个 nuget 包 (Microsoft.Bot.Connector.Teams),它在 SDK 的 V4 中不再可用。但正如我所看到的,您已经从 JSON 输入中获得了一个 conversationId,这应该不是问题。只需使用您在 JSON 中传递的 conversationId。
如果您这样做,您的代码可能类似于:
private static async Task SendProActiveMessgae()private async Task ForwardMessage(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 serviceURL = (string) restCmd["serviceURL"]
var conversationId = (string) restCmd["conversation"];
var uri = new Uri(serviceURL);
var appId = "APP ID";
var appSecret = "APP PASSWORD";
ConnectorClient connector = new ConnectorClient(uri, appId, appSecret);
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 (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
已解决
感谢用户 MarkMoose,我意识到我的数据库 table 没有存储完整的 ID。 未来的调试人员:如果这不能帮助您解决问题,请查看我与 MarkMoose 的对话,他们向我介绍了非常有用的故障排除步骤。
我正在尝试使用 Microsoft Bot SDK 版本 4.0 创建托管在 Azure 上的 Microsoft Teams 机器人。
流程如下
Web 应用警报触发器。向机器人发送 POST 请求,其中包含(从用户之前的消息中收集的所有这些数据)
- 至(收件人用户的)ID
- 收件人姓名(收件人用户)
- 来自 ID(我的机器人)
- 发件人姓名(我的机器人)
- 频道编号
- 对话 ID
- 服务 URL(收件人用户)
Bot 从 JSON 中提取信息形成新消息 activity
- Bot 发送 activity 给用户
问题: 当 bot 尝试使用上面列出的凭据创建 ConversationAccount
对象时,它会抛出以下错误:
Exception caught: Microsoft.Bot.Schema.ErrorResponseException: Operation returned an invalid status code 'BadRequest'
这里是相关部分的代码。
请特别注意以下几行:
var account = new MicrosoftAppCredentials(botCreds["App ID"], botCreds["App Password"]);
var jwtToken = await account.GetTokenAsync();
ConnectorClient connector = new ConnectorClient(new System.Uri(serviceURL), account);
当我找到其他人对我的问题的解决方案时,这些行略有不同。在我当前使用的代码版本中
/// <summary>
/// Sends a message to a user or group chat.
/// </summary>
/// <param name="forwardContext">JSON object containing credentials for destination chat.</param>
/// <param name="messageToSend">The message to forward.</param>
/// <returns></returns>
private async Task ForwardMessage(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 cred_str = $@"toId: {toId}
toName: {toName}
fromId: {fromId}
fromName: {fromName}
channelId: {channelId}
serviceURL: {serviceURL}
conversationId: {conversationId}";
_logger.LogInformation(cred_str);
_logger.LogInformation($"Forwarding the following message to {toName}: {messageToSend}");
Dictionary<string, string> botCreds = GetBotCredentials();
// Create relevant accounts
ChannelAccount userAccount = new ChannelAccount(name: toName, id: toId);
ChannelAccount botAccount = new ChannelAccount(name: fromName, id: fromId);
if (!MicrosoftAppCredentials.IsTrustedServiceUrl(serviceURL))
{
_logger.LogInformation($"Adding to trusted service urls: {serviceURL}");
// Register the service URL as trusted
MicrosoftAppCredentials.TrustServiceUrl(serviceURL);
}
MicrosoftAppCredentials.TrustServiceUrl(serviceURL);
var account = new MicrosoftAppCredentials(botCreds["App ID"], botCreds["App Password"]);
var jwtToken = await account.GetTokenAsync();
ConnectorClient connector = new ConnectorClient(new System.Uri(serviceURL), account);
// Create a new message activity
IMessageActivity message = Activity.CreateMessageActivity();
conversationId = (
await connector
.Conversations
.CreateDirectConversationAsync(botAccount, userAccount)).Id;
// Set relevant message details
message.From = botAccount;
message.Recipient = userAccount;
message.Text = messageToSend;
message.Locale = "en-Us";
message.ChannelId = channelId;
// Create a new converstaion and add it to the message.
message.Conversation = new ConversationAccount(id: conversationId);
await connector.Conversations.SendToConversationAsync((Activity) message);
}
这是我收集上述信息的代码。当用户首次与机器人交互时,将调用此函数。
/// <summary>
/// Called only when the !setup command is sent to the bot.
/// Updates the chats info in the DB.
/// </summary>
/// <param name="activity">Activity of the message the "!setup" command was sent in.</param>
/// <returns>True if the update query executed fine.</returns>
private bool SetupCommand(Activity activity)
{
// Connect to the database
this.database = new DBConnection(serverIP, databaseName, userName, password, _logger);
this.database.Connect();
var tableName = "ms_teams_chats";
// Data gathered from Activity for database.
// User ID
string toId = activity.From.Id;
// User Name
string toName = activity.From.Name;
// Bot ID
string fromId = activity.Recipient.Id;
// Bot Name
string fromName = activity.Recipient.Name;
// Users service URL
string serviceURL = activity.ServiceUrl;
// The platform the message came from. Example: 'skype'
string channelId = activity.ChannelId;
string conversationID = activity.Conversation.Id;
string conversationName = activity.Conversation.Name;
bool isGroupChat = activity.Conversation.IsGroup ?? false;
string upsertQuery = string.Empty;
upsertQuery = $@"
INSERT INTO {tableName}
(user_id, user_name, assoc_bot_id, assoc_bot_name, service_url, channel_id, conversation_id, is_group_chat)
VALUES (
'{toId}', '{toName}', '{fromId}', '{fromName}', '{serviceURL}', '{channelId}', '{conversationID}', {isGroupChat}
)
ON DUPLICATE KEY UPDATE
user_id = '{toId}',
user_name = '{toName}',
assoc_bot_id = '{fromId}',
assoc_bot_name = '{fromName}',
service_url = '{serviceURL}',
channel_id = '{channelId}',
conversation_id = '{conversationID}',
is_group_chat = {isGroupChat}
";
try
{
this.database.ExecuteNonQuery(upsertQuery);
}
catch (System.Exception e)
{
_logger.LogError($"Could not update users information. \nError:{e.ToString()}");
return false;
}
return true;
}
您似乎正在为我上周遇到的同样问题而苦苦挣扎。似乎 CreateDirectConversationAsync 在 MS Teams 中不起作用,因为 Teams 也需要一个 tennantId。 我在这里找到了关于此的声明:https://github.com/Microsoft/BotBuilder/issues/2944
答案提到了一个 nuget 包 (Microsoft.Bot.Connector.Teams),它在 SDK 的 V4 中不再可用。但正如我所看到的,您已经从 JSON 输入中获得了一个 conversationId,这应该不是问题。只需使用您在 JSON 中传递的 conversationId。 如果您这样做,您的代码可能类似于:
private static async Task SendProActiveMessgae()private async Task ForwardMessage(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 serviceURL = (string) restCmd["serviceURL"]
var conversationId = (string) restCmd["conversation"];
var uri = new Uri(serviceURL);
var appId = "APP ID";
var appSecret = "APP PASSWORD";
ConnectorClient connector = new ConnectorClient(uri, appId, appSecret);
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 (Exception ex)
{
Console.WriteLine(ex.Message);
}
}