3

until now I have only ever used the bot to send messages to a channel. You looked like this.

{
  "channel_id": "my-id",
  "message": "Hello Channel"
}

How can I send a simple direct message to a user? The url for the direct Chanel is :"/api/v4/channel/direct" right? so try to send it. as reply i get this "Typo? are you missing a team_id or user_id as part of the url?," Thats okay cause i dont set a reciever User. But now? On Github i just read i need to set 2 ids? My id and the id from the reciever right? How i get the ID from another User?

Dennis Schaffer
  • 556
  • 7
  • 17

2 Answers2

2

You need to use a special channel form with an underscore like X_Y, where X and Y are user ids. User ids are not the same as user names.

payload={"channel": "6w41z1q367dujfaxr1nrykr5oc__94dzjnkd8igafdraw66syi1cde", "text": "Hello, this is some text\nThis is more text. :tada:"}

You can check FAQ page for the details https://docs.mattermost.com/developer/webhooks-incoming.html

Max Prokopov
  • 1,308
  • 11
  • 16
  • 2
    A neat trick to to use the same user ID as X and Y. That way you don't need to create a bot user in Mattermost and the message appears in user's DM to themself. – Harshil Sharma May 02 '20 at 04:04
  • If the DM "channel" has more than two users, the name won't have that particular format. Fortunately, if you have access to the database (or can ask someone who does), you can send to whatever the value is in the `Name` column of the `Channels` table, even if it's not a pretty one. – Walf Mar 14 '23 at 04:41
1

Here's another way, I had this solution

first check if the bot has the correct permissions!! This was my mistake

Then send a Post Request to url+"/api/v4/channels/direct Send Bearer Token in the header and as a body a Json

["bot ID", "reciever ID"]

Now you have to send a second post request. See here: "https://api.mattermost.com/#tag/posts/paths/~1posts/post" This is the same as a message to a channel.

As channel_id you take the "id" you got back from the previous request. Short way: URL "/api/v4/posts" + Bearer Token in the Header This Json in the Body:

{
  "channel_id": "the return id from the first request",
  "message": "Hello Dude, how are you!"
}
Dennis Schaffer
  • 556
  • 7
  • 17