Slash command integration - Where are the parameters?


#1

Hi,

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.

Summary

I cannot see/get my implicit parameters I should see as per https://docs.mattermost.com/developer/slash-commands.html#custom-slash-command when using webtasks.
E.g. the user_name, user_id, channel_name etc.

Issue in one concise sentence

Steps to reproduce

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

Expected behavior

I’d like to see the paramaters as per section 8 in https://docs.mattermost.com/developer/slash-commands.html#custom-slash-command
When using Slack and the very same webtask, it worked immediately.

Observed behavior

let whoAsked = req.query.user_name;

should (for a GET request) show the user_name. user_id did not work either.

I get my text out on the channel. Except where the user name (or ID) should be, it’s “undefined”.


#2

Hey @harald! Thanks for trying out Mattermost.

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.


#3

No error messages. All I can see in the logs which state that I executed a slash command is:

[2018/05/06 03:46:08 UTC] [DEBG] POST - /api/v4/commands/execute
[2018/05/06 03:46:08 UTC] [DEBG] Executing cmd=dice userId=sg6r65fnitnfpc3qodw4uyp38w

Is there a way to display the complete URL which is called for a slash command (incl. body)?


#4

Thanks @harald! Let me touch base with our engineers about this one.


#5

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 :slight_smile:


#6

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.


#7

Good to know the POST requests work. Do you have an example of what a GET request from Slack’s slash commands looks like?


#8

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


#9

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?


#10

Here is a simple one. Using http instead of https.

[Sun May 13 2018 11:42:32 GMT+0000 (UTC)] "GET /?token=gBi77oq3GPr84sGhvOEx1qdH&team_id=TAJ3W83GQ&team_domain=apple-vs-oranges&channel_id=CAJHPQ9B6&channel_name=random&user_id=UAKM844JJ&user_name=harald.kubota&command=%2Fgettest&text=&response_url=https%3A%2F%2Fhooks.slack.com%2Fcommands%2FTAJ3W83GQ%2F362827359971%2FpbZRnmWvgxXMD1NRgvSTFPy9" "Slackbot 1.0 (+https://api.slack.com/robots)"

Compare to Mattermost:

[Sun May 13 2018 11:46:21 GMT+0000 (UTC)] "GET /" "Go-http-client/1.1"


#11

@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.

This will be fixed in the next release of Mattermost as per https://mattermost.atlassian.net/browse/MM-10201.

Sorry about this! I’d recommend sticking with POST until you can upgrade and get this fix.


#12

Thanks, Jesse. I wasn’t aware that we had a ticket to do that already. I was just about to file a new one