Posting incoming webhook messages from a bot account

I’ve successfully set up an incoming webhook, and it posts from my user with the BOT label as documented. But I’d prefer that the webhook post as a dedicated user. I’ve created a new bot account for this purpose, but I see no way to configure the webhook to post as that bot account instead of from my account. Am I missing something? Thanks.

As far as I’m aware you should be able to do what your asking about. I briefly looked it up on the Mattermost documentation, and saw that the command below is provided to use for verifying a bot account has successfully been created:

curl -i -X POST -H 'Content-Type: application/json' -d '{"channel_id":"<channel-id>", "message":"This is a message from a bot", "props":{"attachments": [{"pretext": "Look some text","text": "This is text"}]}}' -H 'Authorization: Bearer <bot-access-token>' <mattermost-url>/api/v4/posts

Have you read the documentation here?

1 Like

The linked doc says,

Only user accounts can create and configure webhooks and slash commands.

This just looks odd.

At laeast the feature request seems to exist.

Allow bot accounts to create webhooks – Mattermost Feature Proposal Forum (uservoice.com)

Hi Mekst and welcome to the Mattermost forums!

An incoming webhook is something that you create for yourself, usually, at least per definition. A webhook is always assigned to a userid and when using the GUI, the userid cannot be modified since it’s inherited from the user creating the webhook because a webhook needs to know what permissions it has. You cannot create a webhook with your user account which is able to post into a private channel you’re not a member of, that’s why. Using the API or the tool mmctl, you can create an incoming webhook which is assigned to a bot account. All you need to know for that is the bots userid and then you can run the following command:

mmctl webhook create-incoming  --channel <channel-id> --user <user-id>

The webhook will be assigned to the account/bot with userid “” then and posts will also show as appearing from this account (+ the bot label next to it).
Please keep in mind that the permission topic I mentioned earlier also applies here, so in order for the webhook to work, make sure that the bot account is allowed to post to the channels you want the webhook to post to.
If the webhook is locked to a channel, just make sure that the bot is a member there, this works always.
If you want the webhook to post to different public channels, you can assign the privilege post:channels to the bot account using the GUI, this will allow the bot to post to all public channels (even if it is not a member of the channel). The privilege post:all allows the bot to also send direct messages to other users and post to private channels where it is a member of.

Hope that helps!

1 Like

I set up the webhook using the bot user ID - however, the messages sent to this webhook are still shown as coming from the bot owner user, not the bot user itself (owner user with bot badge).

Hi @strarsis and welcome to the Mattermost forums!

Can you please run the following mmctl commands (replace the webhook ID with the ID of your webhook) and verify that the username returned is really the name of the bot account you’re expecting?

mmctl --local user search $(mmctl --local  webhook show <WEBHOOKID> --json | jq -r .user_id) --json | jq -r .username
1 Like

The returned username is not the name of the bot account, but rather of the owner account.
So the mmctl webhook create-incoming command that should have created the incombing webhook for that bot account actually created it for the owning user account instead.

This is what I am using for creating a webhook in channel development (team my_team) for bot account uptimekuma (an excellent uptime tool by the way):

channel_id=$(mmctl channel search --team my_team development --json | jq -r .id)
user_id=$(mmctl user list --json | jq -c '.[] | select( .username | contains("uptimekuma"))' | jq -r .id)
mmctl webhook create-incoming --channel "$channel_id" --user "$user_id" --display-name "Uptime Kuma" --description "Uptime Kuma Alerts"

The webhook is successfully created by the command – but when I use your command against the webhook ID to query the owner I get the owner of the bot account, not the bot account itself.

Yes, this is not what you would expect and I’ve also pointed that out some time ago.
You should be able to fix that when creating the webhook on the CLI, I just verified that on my demo system and created a webhook for the playbooks bot account:

# mmctl --local user list | grep playbooks
ieaiw1cdairo5x6g7p3oq6hhha: playbooks (playbooks@localhost)

# mmctl --local webhook create-incoming --channel jitoep1mcfrtjjp8facxycqeec --user ieaiw1cdairo5x6g7p3oq6hhha --display-name test123
Id: ywdjwo9w1ibcdg9a8icrpf3onw
Display Name: test123

# mmctl --local user search $(mmctl --local  webhook show ywdjwo9w1ibcdg9a8icrpf3onw --json | jq -r .user_id) --json | jq -r .username
playbooks