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

时间:2017-11-23 15:01:28

标签: node.js botframework microsoft-teams

我的机器人与docs quickstart中演示的机器人相同。它会重复用户所说的内容(暂时)。

它目前在本地运行并使用ngrok公开。我已经使用Microsoft Bot Framework注册了机器人。

我已经在Microsoft Bot Framework中配置了Microsoft Teams频道,并且我将我的机器人安装到了团队中。我的机器人可以从团队用户那里接收消息

目前,僵尸程序只会重复向用户收到的任何内容,但我想要它做的是发布到Microsoft Teams频道。我希望它在没有用户首先提示的情况下发布到团队频道 - 而不是用户。因此,例如,给定某个条件(例如,由某些事件触发,例如时间,拉取请求等),它会在频道中发布消息。

我已经阅读了有关sending proactive messages的文档,我认为为了向团队频道发送消息,机器人需要知道"地址"用户。此信息存储在session.message.address对象中,它从当前对话中获取。但是,在我的情况下,我没有“当前的保护”,因为我不想只回复用户,我想主动在频道中发帖。

那么,如何为团队频道永久设置必要的凭证/地址/会话数据?

我已经研究过的事情:

  • 网络挂接。我已在我的Teams频道中配置了一个webhook,我可以使用curl轻松地向其发送消息(使用webhook网址)。所以我只需要一个网址(不需要身份验证)就可以向Teams频道发送简单的消息,但我不确定我是如何将这个网址添加到我的机器人中的。

  • How do we maintain different session for different users in Microsoft Bot Framework?我不确定这里的答案是否回答我的问题。我的问题是机器人正在启动对话,而不是团队用户,因此我需要能够自己设置会话数据,以便机器人知道去哪里。

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);
});

3 个答案:

答案 0 :(得分:4)

对于任何想为c#做到这一点的人,以下是对我有用的解决方案:

rpt_cursor = rpt_conn.cursor()
sql = """SELECT `field` FROM `db`.`table`;"""
rpt_cursor.execute(sql)

#Creating list of field names.
num_fields = len(rpt_cursor.description)
field_names = [i[0] for i in rpt_cursor.description]

# getting results and defining dict to load them into.
results = rpt_cursor.fetchall()
dictofemails = []

print('These are the SQL results....')
print(results)  # These look fine.

# Appending data to the dict.
for row in results:
    dictofdata.append(dict(zip(field_names,row)))

print('These are the dict results...')
print(dictofdata) # Once again this looks like a fine dict with a format of [{'field_name' : 'xyz'}, {'field_name' : 'abc'}].

api_request_url = 'https://api.domainname.com/api/list/' + str(target) +'/Api_Key/' + my_apikey

print('api_request_url')  # This looks fine.

response = requests.put(api_request_url, headers=headers, data=dictofdata)
print(response)
print(response.content)

注意:如果要在Bot的控制器代码之外调用此代码,则需要在serviceUrl上调用TrustServiceUrl,如下所示:

Traceback (most recent call last):
File "NGM_ListMaker_WeeklyBounceLoad.py", line 306, in <module>
response = requests.put(api_request_url, headers=headers, data=dictofemails)
File "C:\Program Files (x86)\Python36-32\lib\site-packages\requests\api.py", line 126, in put
return request('put', url, data=data, **kwargs)
File "C:\Program Files (x86)\Python36-32\lib\site-packages\requests\api.py", line 58, in request
return session.request(method=method, url=url, **kwargs)
File "C:\Program Files (x86)\Python36-32\lib\site-packages\requests\sessions.py", line 494, in request
prep = self.prepare_request(req)
File "C:\Program Files (x86)\Python36-32\lib\site-packages\requests\sessions.py", line 437, in prepare_request
hooks=merge_hooks(request.hooks, self.hooks),
File "C:\Program Files (x86)\Python36-32\lib\site-packages\requests\models.py", line 308, in prepare
self.prepare_body(data, files, json)
File "C:\Program Files (x86)\Python36-32\lib\site-packages\requests\models.py", line 499, in prepare_body
body = self._encode_params(data)
File "C:\Program Files (x86)\Python36-32\lib\site-packages\requests\models.py", line 97, in _encode_params
for k, vs in to_key_val_list(data):

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

答案 1 :(得分:2)

绝对有可能。我们称之为主动消息,并且可以主动向用户和频道发送消息。

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

要向频道发送主动消息,您需要使用Microsoft Teams SDK(如这些示例所示)。

最后但并非最不重要的一点是,您需要将机器人添加到团队中,以便向团队中的某个渠道发送消息,这需要清单。

答案 2 :(得分:0)

希望这对您有用。.下面的代码在发起聊天之前主动将消息发送到会话。

 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);    
       }
    });