简体   繁体   English

如何让我的机器人向Microsoft Teams频道发布消息?

[英]How can I get my bot to post a message to a Microsoft Teams channel?

I have a bot that is identical to the one demonstrated in the docs quickstart . 我的机器人与文档快速入门中演示的机器人相同。 It repeats back whatever the user says (for now). 无论用户说什么(现在),它都会重复。

It is currently running locally and exposed with ngrok. 它目前在本地运行,并使用ngrok进行曝光。 I've registered the bot with the Microsoft Bot Framework. 我已经使用Microsoft Bot Framework注册了bot。

I have configured the Microsoft Teams channel in the Microsoft Bot Framework, and I've sideloaded my bot into Teams. 我已经在Microsoft Bot Framework中配置了Microsoft Teams频道,并且我将我的机器人安装到了团队中。 My bot can receive messages from Teams users. 我的机器人可以团队用户接收消息。

At present, the bot just repeats whatever it receives back to the user, but what I want it to do is post to a Microsoft Teams channel . 目前,僵尸程序只是将它收到的任何内容重复给用户,但我想要它做的是发布到Microsoft Teams频道 I want it to post to a Teams channel - not a user - without being prompted first by a user. 我希望它在没有用户首先提示的情况下发布到团队频道 - 而不是用户。 So for example given a certain condition (eg. triggered by some event such as time of day, a pull request, etc.) it posts a message in a channel. 因此,例如,给定某个条件(例如,由某些事件触发,例如时间,拉取请求等),它在频道中发布消息。

I've read the documentation about sending proactive messages , and I gather that in order to send a message to a teams channel, the bot needs to know the "address" of the user. 我已经阅读了有关发送主动消息的文档,我认为为了向团队频道发送消息,机器人需要知道用户的“地址”。 This information is stored in the session.message.address object, and it gets this from the current conversation. 此信息存储在session.message.address对象中,它从当前对话中获取。 However, in my case I don't have a 'current conservation', because I don't want to just respond to a user, I want to post in a channel proactively. 但是,在我的情况下,我没有“当前保护”,因为我不想只回应用户,我想主动在频道中发帖。

So, how do I permanently set the necessary credentials/address/session-data for the Teams channel? 那么,如何为Teams频道永久设置必要的凭证/地址/会话数据?

Things I've looked into: 我调查过的事情:

  • Webhooks. 网络挂接。 I've configured a webhook in my Teams channel, and I can send it a message easily enough (using the webhook url) using curl . 我在我的Teams频道中配置了一个webhook,我可以使用curl轻松地发送消息(使用webhook url)。 So I can send the Teams channel a simple message with just a url (no authentication required), but I'm not sure how I'd get this url into my bot. 因此,我可以向团队频道发送一个简单的消息,只需一个网址(不需要身份验证),但我不确定如何将此网址添加到我的机器人中。

  • How do we maintain different session for different users in Microsoft Bot Framework? 我们如何为Microsoft Bot Framework中的不同用户维护不同的会话? I'm not sure that the answer here answers my question. 我不确定这里的答案是否能回答我的问题。 My problem is that the bot is initiating the 'conversation', not a Teams user, so I need to be able to set the session data myself so the bot knows where to go. 我的问题是机器人正在启动“对话”,而不是团队用户,因此我需要能够自己设置会话数据,以便机器人知道去哪里。

App.js: App.js:

require('dotenv').config();
var restify = require('restify');
var builder = require('botbuilder');

// Setup Restify Server
var server = restify.createServer();
server.listen(process.env.port || process.env.PORT || 3978, function () {
   console.log('%s listening to %s', server.name, server.url); 
});

// Create chat connector for communicating with the Bot Framework Service
var connector = new builder.ChatConnector({
    appId: process.env.MICROSOFT_APP_ID,
    appPassword: process.env.MICROSOFT_APP_PASSWORD    
});

// Listen for messages from users 
server.post('/api/messages', connector.listen());

// Receive messages from the user and respond by echoing each message back (prefixed with 'You said:')
var bot = new builder.UniversalBot(connector, function (session) {
    session.send("You said: %s", session.message.text);
});

For anyone who is wondering about the same for c#, here is the solution that worked for me: 对于那些对c#感到疑惑的人来说,这里有适合我的解决方案:

    var channelData = context.Activity.GetChannelData<TeamsChannelData>();
    var message = Activity.CreateMessageActivity();
    message.Text = "Hello World";

    var conversationParameters = new ConversationParameters
    {
         IsGroup = true,
         ChannelData = new TeamsChannelData
          {
             Channel = new ChannelInfo(channelData.Channel.Id),
         },
         Activity = (Activity) message
    };

    var connectorClient = new ConnectorClient(new Uri(activity.ServiceUrl));
    var response = await 
    connectorClient.Conversations.CreateConversationAsync(conversationParameters);

Note: If you are calling this outside Bot's controller code then you need to call TrustServiceUrl on serviceUrl as shown here: 注意:如果你在Bot的控制器代码之外调用它,那么你需要在serviceUrl上调用TrustServiceUrl,如下所示:

    MicrosoftAppCredentials.TrustServiceUrl(serviceUrl, DateTime.MaxValue);
    var connectorClient = new ConnectorClient(new Uri(serviceUrl));

Source of answer: https://github.com/OfficeDev/BotBuilder-MicrosoftTeams/issues/162 答案来源: https//github.com/OfficeDev/BotBuilder-MicrosoftTeams/issues/162

It is definitely possible. 这绝对是可能的。 We call these proactive messages and it's possible to proactively message both users and channels. 我们将这些主动消息称为主动消息,并且可以主动向用户和频道发送消息。

For the latter, see the sample at https://github.com/OfficeDev/microsoft-teams-sample-complete-node , specifically this file, ProactiveMsgToChannelDialog.ts . 对于后者,请参阅https://github.com/OfficeDev/microsoft-teams-sample-complete-node上的示例,特别是此文件ProactiveMsgToChannelDialog.ts

To send proactive messages to channels, you need to use the Microsoft Teams SDK (as these samples do). 要将主动消息发送到频道,您需要使用Microsoft Teams SDK(如这些示例所示)。

Last but not least, you need to add the bot to a team in order to send a message to one of the channels in the team, which requires a manifest. 最后但同样重要的是,您需要将团队添加到团队中,以便向团队中的某个渠道发送消息,这需要清单。

Hope this works for you.. below code proactively sends the message to session before initiating the chat. 希望这对您有用..下面的代码在启动聊天之前主动将消息发送到会话。

 bot.on('conversationUpdate', function (message) {  
       if (message.membersAdded[0].id === message.address.bot.id) {             
             var reply = new builder.Message()    
                   .address(message.address)               
                   .text("Hello"");        
             bot.send(reply);    
       }
    }); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM