Reputation: 1437
I've been banging my head on this for too long;
I want a setup where
I've managed to
I haven't managed to
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.
Upvotes: 2
Views: 1218
Reputation: 1437
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);
This will give you a JSON formatted string to work with! Yass!
Upvotes: 1