Search code examples
mattermost

Mattermost API create a direct message channel with powershell


I can't find enough documentation or a working example for this.

From the official website it seems to be possible, I tried this:

$Payload = { text:Hello; username:bot; channel:user}
Invoke-RestMethod -Uri https://mymattermost.com/hooks/%Bearer token% -Method Post 
-ContentType 'application/json' -Body (ConvertTo-Json $Payload

but always get same error:

Invoke-RestMethod : {"id":"web.incoming_webhook.text.app_error","message":"No text
specified","detailed_error":"","request_id":"aaaaaaaaaaaaaaa","status_code":400}
At line:1 char:1

Solution

  • Well, all you have to do is to add @ for the user name under the channels and it works, like this:

    > $Payload = @{ text="Hello"; username="bot"; channel="@user"}
    
    > Invoke-RestMethod -Uri https://mymattermost.com/hooks/124twgvrq4tgw54e4 -Method Post 
      -ContentType 'application/json' -Body (ConvertTo-Json $Payload) -UseBasicParsing
    

    This will do the trick. Note: whoever created the webhook, will be the user assigned to a direct channel