路由附件
Routing attachments
我正在尝试将发送到 bot 的附件从一个用户路由到另一个用户。当我试图从模拟器发送东西到 emulator/telegram/skype 时它工作正常,但是当我试图从 skype/telegram 发送它时它根本不起作用(skype 什么也没说,电报争论:"POST to mybot failed: POST to the bot's endpoint failed with HTTP status 500").我应该去哪里寻找问题?
这是我的代码:
if (mes.Attachments != null && mes.Attachments.Any())
{
var list = new List<Attachment>();
foreach (var attachment in mes.Attachments)
{
using (HttpClient httpClient = new HttpClient())
{
// Skype attachment URLs are secured by a JwtToken, so we need to pass the token from our bot.
if (mes.ChannelId.Equals("skype", StringComparison.InvariantCultureIgnoreCase) &&
new Uri(attachment.ContentUrl).Host.EndsWith("skype.com"))
{
var mstoken = await new MicrosoftAppCredentials().GetTokenAsync();
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer",
mstoken);
}
var responseMessage = await httpClient.GetAsync(attachment.ContentUrl);
var ms = await ConvertContentToByteArray(responseMessage);
var sendAttachment = new Attachment()
{
ContentType = attachment.ContentType,
Name = attachment.Name,
ContentUrl = String.Format("data:image/png;base64,{1}", attachment.ContentType,
Convert.ToBase64String(ms))
};
list.Add(sendAttachment);
}
}
message.Attachments = list;
message.AttachmentLayout = mes.AttachmentLayout;
}
await connector.Conversations.SendToConversationAsync((Activity)message);
"mes" 是传入消息,“消息 - 输出。
提前感谢您的回答!
更新 1:我设法用 sending/receiving 文件解决了我的问题(请参阅下面的回答)。不幸的是,出现了文件大小限制的新问题(参见 )。
上面的代码是正确的并且工作正常(如果附件的大小不是太大)。我的问题(也是愚蠢的错误)是在其他地方使用 mes.Text 而没有进行空值检查。 Skype/Telegram 中的附件有 mes.Text == null。
我正在尝试将发送到 bot 的附件从一个用户路由到另一个用户。当我试图从模拟器发送东西到 emulator/telegram/skype 时它工作正常,但是当我试图从 skype/telegram 发送它时它根本不起作用(skype 什么也没说,电报争论:"POST to mybot failed: POST to the bot's endpoint failed with HTTP status 500").我应该去哪里寻找问题?
这是我的代码:
if (mes.Attachments != null && mes.Attachments.Any())
{
var list = new List<Attachment>();
foreach (var attachment in mes.Attachments)
{
using (HttpClient httpClient = new HttpClient())
{
// Skype attachment URLs are secured by a JwtToken, so we need to pass the token from our bot.
if (mes.ChannelId.Equals("skype", StringComparison.InvariantCultureIgnoreCase) &&
new Uri(attachment.ContentUrl).Host.EndsWith("skype.com"))
{
var mstoken = await new MicrosoftAppCredentials().GetTokenAsync();
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer",
mstoken);
}
var responseMessage = await httpClient.GetAsync(attachment.ContentUrl);
var ms = await ConvertContentToByteArray(responseMessage);
var sendAttachment = new Attachment()
{
ContentType = attachment.ContentType,
Name = attachment.Name,
ContentUrl = String.Format("data:image/png;base64,{1}", attachment.ContentType,
Convert.ToBase64String(ms))
};
list.Add(sendAttachment);
}
}
message.Attachments = list;
message.AttachmentLayout = mes.AttachmentLayout;
}
await connector.Conversations.SendToConversationAsync((Activity)message);
"mes" 是传入消息,“消息 - 输出。
提前感谢您的回答!
更新 1:我设法用 sending/receiving 文件解决了我的问题(请参阅下面的回答)。不幸的是,出现了文件大小限制的新问题(参见
上面的代码是正确的并且工作正常(如果附件的大小不是太大)。我的问题(也是愚蠢的错误)是在其他地方使用 mes.Text 而没有进行空值检查。 Skype/Telegram 中的附件有 mes.Text == null。