Thursday, 18 May 2017

How to get a message from self hosted custom Microsoft Teams bot?

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

I want a setup where

  • 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

I've managed to

  • Code in Node.js
  • Run a local server (either with restify or https)
  • I've managed to get a request sent to my Node.js implementation

I haven't managed to

  • Catch the actual message string or other useful information when the bot is mentioned

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

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

When doing the following switch of code

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

The result is

ERROR: ChatConnector: receive - no security token sent.

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.



via shellström

No comments:

Post a Comment