1

How i can send direct message from mattermost bot to user? I find similar question, but it not works for me:

package main

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

func main() {

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

    user, resp := client.GetUserByUsername("UsernameOf.UserToMessage", "here.my.user.name")
    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
    }

}

i try to run with go run *.go script start without errors, but nothin happens(not get message from bot), where is my mistake?

Thanks!

mocart
  • 389
  • 1
  • 6
  • 16
  • Are you logging the errors anywhere? I see the `return`s, but are the messages being printed to console or a log file? There might be something in there that gives away what the exact issue is. – Francis Bartkowiak Feb 28 '23 at 15:19

1 Answers1

0

I found solution, in my MM server is used newer version of API, just change:

"github.com/mattermost/mattermost-server/v5/model"

to

"github.com/mattermost/mattermost-server/v6/model"

Thanks!

mocart
  • 389
  • 1
  • 6
  • 16