[Solved] Sending message in Mattermost with priority from Go

I want to send message with priority from my bot on GO. I using github.com/mattermost/mattermost-server/v6/model module. I tried many different options, but nothing worked. All messages come as usual, without priority, as it can be done in the GUI of the native client.

Here is the code from my last attempt. Can you please tell me how to solve this problem?

package main

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

func sendMessage(logger zerolog.Logger, client *model.Client4, message string, channelID string, priority string) error {

    post := &model.Post{
        ChannelId: channelID,
        Message:   message,
    }
    post.AddProp("priority", priority)

    _, resp, err := client.CreatePost(post)
    if err != nil {
        logger.Error().Err(err).Msg("Message sending error")
        return fmt.Errorf("Message sending error: %v", err)
    }

    if resp.StatusCode != 201 {
        logger.Error().Int("status", resp.StatusCode).Msg("Failed to send message")
        return fmt.Errorf("Failed to send message. Status: %d", resp.StatusCode)
    }

    return nil
}

.....

func main() {
.....
    err := sendMessage(logger.With().Str("channel", ch.Name).Logger(), client, message, ch.ChannelID, "urgent")
.....
}

I tried using a newer module - github.com/mattermost/mattermost/server/public/model. Nothing worked either.

Thank you very much in advance!

Hi @dstarodub, it looks like you’re on the right track! While Mattermost doesn’t currently support a native “priority” property for posts sent via the API, you might achieve a similar effect by using custom post properties and incorporating the priority into the post’s content or metadata for your bot to handle it accordingly. Let us know if you need further assistance! :blush:

Use this:

&model.Post{
	ChannelId: channelID,
	Message:   message,
	Metadata: &model.PostMetadata{
		Priority: &model.PostPriority{
			Priority: model.NewPointer(model.PostPriorityUrgent),
		},
	},
}
1 Like

Yes. Yesterday I find this solution:

post := &model.Post{
		ChannelId: channelID,
		Message:   message,
		Metadata: &model.PostMetadata{
			Priority: &model.PostPriority{Priority: &priority},
		},
	}
1 Like