I hope this is not a trivial and well known problem. I am testing MatterMost for 2 days only, but I would say I did my homework searching via Google in many places for an answer for my problem.
MatterMost v4.9.1. Using docker-compose to run on a server I have. There’s no error coming out of the docker-compose logs.
I’m using GET requests. POST did not work either.
Here the exact webtask code:
var express = require(‘express’);
var Webtask = require(‘webtask-tools’);
var bodyParser = require(‘body-parser’);
var app = express();
app.use(bodyParser.json());
app.get('/', function (req, res) {
//console.log(req.headers);
let diceNumber = Math.floor(Math.random()*6+1);
let whoAsked = req.query.user_name;
res.status(200).send({"response_type": "in_channel", "text": `${whoAsked} rolled the dice and got a ${diceNumber}`});
});
module.exports = Webtask.fromExpress(app);
Do you see any error messages in System Console > Logs? You may first need to set System Console > Logging > Enable Webhook Debugging to true and set System Console > Logging > Console Log Level to DEBUG.
Not sure this helps or is more confusing:
Using Restify and NodeJS at work with its Mattermost server (testing at work, not installed by me), I could access the user_name (and similar) parameters via req.body.user_name when using POST.
When using GET that did expectedly not work. However neither did req.query.user_name.
I guess I’ll have to learn how to read the source code of Mattermost
Ok, found something which makes a lot of sense: bodyparser.json() is not the thing to use, but instead bodyparser.urlencoded(). With POST method this works as expected:
[...]app.use(bodyParser.urlencoded());
app.post('/', function (req, res) {
let whoAsked = req.body.user_name; [...]
I still cannot GET work at all. Is it maybe totally broken? I have found no evidence that the parameters like user_name etc. are in the URL. They always seem to be in the body.
Here the complete and working code which does a GET request.
Working for Slack. Not working for Mattermost:
var express = require('express');
var Webtask = require('webtask-tools');
var bodyParser = require('body-parser');
var app = express();
app.use(bodyParser.urlencoded());
app.get('/', function (req, res) {
let diceNumber = Math.floor(Math.random()*6+1);
let whoAsked = req.query.user_name;
res.status(200).send({"response_type": "in_channel", "text": `${whoAsked} rolled the dice and got a ${diceNumber}`});
});
module.exports = Webtask.fromExpress(app);
Output in a Slack channel: harald.kubota rolled the dice and got a 2
Output in a Mattermost channel: undefined rolled the dice and got a 2
Okay, so we’re not sending any of the query parameters that Slack does. Do you know if there’s a list of all possible query parameters somewhere? Or can you send us the full URL that Slack sent the request to in your example?
@harald, there was a bug with our implementation of slash commands that, when configured for GET, still sent the payload in the body instead of the querystring.