How Do You Send A Direct Message In Mattermost From A Bot Using The Go Driver?

Francis Bartkowiak

Using Mattermost's Go Driver, is it possible to send a Direct Message from a bot account to a user? I have been trying this method below, but I keep getting the error: "You do not have the appropriate permissions.," I've checked the permissions of the bot multiple times, and it should be able to send the message. I've confirmed that it can also send the message to public channels, so what am I doing wrong?

package main

import (
    "github.com/mattermost/mattermost-server/v5/model"
)

func main() {

    client := model.NewAPIv4Client("https://server.name.here")
    client.SetToken("Bots-Token-Here")
    bot, resp := client.GetUserByUsername("NameOf.BotSendingMessage", "")
    if resp.Error != nil {
        return
    }

    user, resp := client.GetUserByUsername("UsernameOf.UserToMessage", "")
    if resp.Error != nil {
        return
    }

    channelId := model.GetDMNameFromIds(bot.Id, user.Id)

    post := &model.Post{}
    post.ChannelId = channelId
    post.Message = "some message"

    if _, resp := client.CreatePost(post); resp.Error != nil {
        return
    }

}
Francis Bartkowiak

It is possible, but you have to create the channel, not just the Channel ID. The snippet that does this looks like this:

channel, resp := client.CreateDirectChannel("firstId", "secondId")
if resp.Error != nil {
    return
}

A working version of the code can be seen here:

package main

import (
    "github.com/mattermost/mattermost-server/v5/model"
)

func main() {

    client := model.NewAPIv4Client("https://server.name.here")
    client.SetToken("Bots-Token-Here")
    bot, resp := client.GetUserByUsername("NameOf.BotSendingMessage", "")
    if resp.Error != nil {
        return
    }

    user, resp := client.GetUserByUsername("UsernameOf.UserToMessage", "")
    if resp.Error != nil {
        return
    }

    channel, resp := client.CreateDirectChannel(bot.Id, user.Id)
    if resp.Error != nil {
        return
    }
    post := &model.Post{}
    post.ChannelId = channel.Id
    post.Message = "some message"

    if _, resp := client.CreatePost(post); resp.Error != nil {
        return
    }

}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

What's the official way of logging in with a personal access token using the client4 go driver for mattermost?

how do you send email from R

How do you send a message using pyOSC?

Using Laravel's Mailgun driver, how do you (gracefully) send custom data and tags with your email?

How do you make a bot send a message to an specific guild in a specific channel without using commands

How to send message using Bale bot into group?

How do you send a pdf file using bot framework on a Skype channel?

How do you tell if the message is from send or sendSync in the main process?

How do you send a txt message to a cell phone from a Lucee web app?

Using a Discord bot, how do I get a string of an embed message from another bot

How to send message followed by start to a bot by link using telethon?

How to send a direct message with Twython?

How do you get the message from PlatformException?

How do you send message using NFC with Xamarin.Android?

How to send a message to the bot inviter

How I can send a message on Facebook using Selenium Driver in Python

How do you use PHP to send form data as a message?

Node.js: How to send a direct message in Slack with MS Bot Framework?

discord python bot - how to send a message not using on_message

How to send message to a specific user from discord bot using discor.js

How do I send a message when my bot disconnects?

How do you make a discord bot that send a message if another user send a message? discord.py

Can't send direct message to Slack bot, feature turned off

How do you make a bot go a line down in a message?

How do you send or forward messages using python-telegram-bot?

How do I get my Discord bot to send a welcome message?

How to send a message from a Microsoft Teams bot using Microsoft Graph?

How do I copy a message and send it again with my discord bot?

How to send message to a telegram bot using telebot api or telegram api