Jos*_*ose 5 .net c# powershell microsoft-teams
我可以从C#应用程序或PS脚本创建传入的Webhook,将JSON消息发送到MSFT文档说明之类的通道。
但是,我想使用传入的Webhook像Slack允许的那样从我的应用程序向用户发送JSON消息(作为私有消息)。
据我所知,MSFT团队无法做到这一点:https : //dev.outlook.com/Connectors/Reference
但是也许您知道任何解决方法或类似的解决方法。
提前致谢 :)
[编辑]用于通过C#App将消息发布到MSFT团队的代码:
//Post a message using simple strings
public void PostMessage(string text, string title)
{
Payload payload = new Payload()
{
Title = title
Text = test
};
PostMessage(payload);
}
//Post a message using a Payload object
public async void PostMessage(Payload payload)
{
string payloadJson = JsonConvert.SerializeObject(payload);
var content = new StringContent(payloadJson);
content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
var client = new HttpClient();
uri = new Uri(GeneralConstants.TeamsURI);
await client.PostAsync(uri, content);
}
Run Code Online (Sandbox Code Playgroud)
达到目标的最佳方法是创建一个Bot并将其实现以暴露Webhook端点,您的应用程序或服务可以将其发布到该Webhook端点,并且该bot可以将这些消息发布到与用户聊天。
首先,根据您的漫游器收到的传入活动,捕获成功发布到漫游器与用户的对话所需的信息。
var callBackInfo = new CallbackInfo()
{
ConversationId = activity.Conversation.Id,
ServiceUrl = activity.ServiceUrl
};
Run Code Online (Sandbox Code Playgroud)
然后将callBackInfo打包到令牌中,该令牌稍后将用作Webhook的参数。
var token = Convert.ToBase64String(
Encoding.Default.GetBytes(
JsonConvert.SerializeObject(callBackInfo)));
var webhookUrl = host + "/v1/hook/" + token;
Run Code Online (Sandbox Code Playgroud)
最后,实现webhook处理程序以解压缩callBackInfo:
var jsonString = Encoding.Default.GetString(Convert.FromBase64String(token));
var callbackInfo = JsonConvert.DeserializeObject<CallbackInfo>(jsonString);
Run Code Online (Sandbox Code Playgroud)
并发布到机器人与用户的对话中:
ConnectorClient connector = new ConnectorClient(new Uri(callbackInfo.ServiceUrl));
var newMessage = Activity.CreateMessageActivity();
newMessage.Type = ActivityTypes.Message;
newMessage.Conversation = new ConversationAccount(id: callbackInfo.ConversationId);
newMessage.TextFormat = "xml";
newMessage.Text = message.Text;
await connector.Conversations.SendToConversationAsync(newMessage as Activity);
Run Code Online (Sandbox Code Playgroud)
在这里查看有关此主题的博客文章。如果您以前从未编写过Microsoft Teams机器人程序,请在此处查看我的其他博客文章,其中包含分步说明。
| 归档时间: |
|
| 查看次数: |
3974 次 |
| 最近记录: |