如何从自托管的自定义Microsoft Teams机器人获取消息?

问题描述:

我已经为此努力了很长时间;

I've been banging my head on this for too long;

我要在哪里设置

  • 我在Node.js中编写代码
  • 我运行本地服务器
  • 在我可以回复的频道中,通过提及回复自定义漫游器
  • I code in Node.js
  • I run a local server
  • Catch replies from mentions to a custom bot in a channel that I can respond to

我设法

  • Node.js中的代码
  • 运行本地服务器(使用restify或https)
  • 我设法将请求发送到我的Node.js实现

我没有设法

  • 提到机器人时,捕获实际的消息字符串或其他有用信息

我在没有多大运气的情况下搜寻了多种资源,这将以最简单的形式解释您.旋转一个Node.js应用程序,该应用程序侦听来自自定义bot的传入请求,对其进行解析以获取消息字符串,然后将其发送回通道.

I have scoured multiple resources without much luck, that would explain how you, in its simplest form; spin up a Node.js app that listens on incoming requests from a custom bot, parses it to fetch the message string and then sends it back to the channel.

这里有一些代码可以转储我得到的响应

Here's some code that dumps the response I get

const fs = require('fs');
var restify = require('restify');
var builder = require('botbuilder');

const https_options = {
    key: fs.readFileSync('[redacted].key'),
    cert: fs.readFileSync('[redacted].pem')
};

// Setup Restify Server
var server = restify.createServer(https_options);

server.listen(process.env.port || process.env.PORT || 8080, 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', function (request, response, next) {
    console.log(request);
});

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

这使我在命中端点时获得了1850行JSON格式的控制台输出(这也意味着该机器人至少正在捕获该请求.但是,与消息相对应的数据中没有任何内容,类似于所发现的示例在此处提到的示例入站消息"中 https://msdn.microsoft.com/zh-cn/microsoft-teams/custombot

This gives me an 1850 row JSON formatted console output when the endpoint is hit (which also means the bot is at least catching the request. But there's nothing in the data that corresponds to a message, similar to that of the example found in "Example inbound message" mentioned here https://msdn.microsoft.com/en-us/microsoft-teams/custombot

在执行以下代码切换时

---- replacing this ----
// Listen for messages from users
server.post('/api/messages', function (request, response, next) {
    logger.debug(request);
});
---- with this ----
// Listen for messages from users
server.post('/api/messages', connector.listen());

结果是

错误:ChatConnector:接收-没有发送安全令牌.

ERROR: ChatConnector: receive - no security token sent.

我怀疑这与我试图用为Office Store制作的连接器解析自定义漫游器的请求有关.我对将此机器人发布到任何商店都不感兴趣.我只需要一个可以响应并响应消息的自托管机器人即可.

I suspect this has something to do with that I'm trying to parse a custom bot's request with a connector that's made for the Office Store. I'm not interested in any publishing of this bot to any store. I simply need a self hosted bot that can react and respond to messages.

我是在错误的位置看还是不正确?关于自定义漫游器的讨论很少,我保证我将做示例代码来描述如果最终有什么效果的话,如何处理这种情况.

Am I looking in the wrong places or not groking this right? There's so little talk about custom bots and I promise I'll do sample code to describe how to deal with this scenario if there's something that works in the end.

最后,我发现 https://stackoverflow.com/a/8640308/975641 在StackOverflow上终于使我步入正轨!

In the end I found https://stackoverflow.com/a/8640308/975641 on StackOverflow that finally got me on track!

获取消息以进行处理的最终结果

The final result for getting the message for processing

const fs = require('fs');
var restify = require('restify');
const https_options = {
    key: fs.readFileSync('[redacted].key'),
    cert: fs.readFileSync('[redacted].pem')
};

var handler = function (request, res) {
    if (request.method === 'POST') {
        var body = '';
        request.on('data', function (data) {
            body += data;
            // 1e6 === 1 * Math.pow(10, 6) === 1 * 1000000 ~~~ 1MB
            if (body.length > 1e6) {
                // FLOOD ATTACK OR FAULTY CLIENT, NUKE REQUEST
                request.connection.destroy();
            }
        });
        request.on('end', function () {

            console.log(body)
            // use POST

        });
    }

    res.writeHead(200, {'Content-Type': 'application/json'});
    //res.end(parseCommands(req));
    res.end(`{
    "type": "message",
    "text": "This is a reply!"
    }`);
};

// Setup Restify Server
var server = restify.createServer(https_options);

server.listen(process.env.port || process.env.PORT || 8080, function () {
    console.log('%s listening to %s', server.name, server.url);
});

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

这将为您提供JSON格式的字符串以供使用!亚斯!

This will give you a JSON formatted string to work with! Yass!