I set it to allow overwriting of Mattermost's username and icon, and posted it to Mattermost using Python.
I had to use a webhook to throw a message to the self-hosted Mattermost, so I investigated it. The project I'm currently working on makes heavy use of Python, so I tried to find out if it could be done with Python.
Posting a message to Mattermost simply throws an http post, so I was wondering if I needed a special library. When I looked it up, I found a library mattermostdriver that looks good, so I decided to use that.
Easy to use
% poetry add mattermostdriver
#For pip:
% pip install mattermostdriver
mattermost_driver = Driver({
'scheme': 'https',
'url': 'mattermost.your-domain.com',
'port': 443,
})
webhook_id = "xxxxxxxxxxxxxxxxxxxxxxxxxx"
mattermost_driver.webhooks.call_webhook(hook_id=webhook_id, options={
"username": "my-bot",
"icon_url": "https://www.mattermost.org/wp-content/uploads/2016/04/icon.png ",
"text": "Hello, this is some text\nThis is from python script. :tada:",
})
You can now post from python
There were quite a few stumbling points, For some reason, even if I specified username and icon_url, it didn't change.
After a little research, it was necessary to change (+ restart) the config of mattermost. The following was detailed.
I changed the following settings and redeployed mattermost server.
/mattermost/config/config.json
"EnablePostUsernameOverride": true,
"EnablePostIconOverride": true,
After changing the settings, I confirmed that the username and icon were changed in the script created in Python.
You can see the same content from here.
That's it.
Recommended Posts