Python mattermost example
py
from mattermostdriver import Driver
TOKEN= "<YOUR_TOKEN>"
URL = ""
def send_message_to_channel(channel, message):
"""Send a message to a channel.
:param channel: Channel name
:type channel: str
:param message: Message to send
:type message: str
"""
driver = Driver({
'url': URL,
'token': TOKEN,
'scheme': 'https',
'port': 443,
})
driver.login()
driver.posts.create_post(options={
'channel_id':channel,
'message': message
})
def get_userid_by_username(username):
"""Get user id by username.
:param username: Username
:type username: str
:return: User id
:rtype: str
"""
driver = Driver({
'url': URL,
'token': TOKEN,
'scheme': 'https',
'port': 443,
})
driver.login()
user = driver.users.get_user_by_username(username)
return user
def send_message_to_user(user, message):
"""Send a message to a user.
:param user: User name
:type user: str
:param message: Message to send
"""
driver = Driver({
'url': URL,
'token': TOKEN,
'scheme': 'https',
'port': 443,
})
driver.login()
driver.posts.create_post(options={
'user_id': get_userid_by_username(user),
'message': message
})
def get_latest_message_in_channel(channel):
"""Get latest message in channel.
:param channel: Channel name
:type channel: str
:return: Latest message
:rtype: str
"""
driver = Driver({
'url': URL,
'token': TOKEN,
'scheme': 'https',
'port': 443,
})
driver.login()
posts = driver.posts.get_posts_for_channel(channel)
return posts.get('posts')[posts.get('order')[0]]
def update_message_in_channel(channel, message, message_id):
"""Update message in channel.
:param channel: Channel name
:type channel: str
:param message: Message to update
:type message: str
:param message_id: Message id
:type message_id: str
"""
driver = Driver({
'url': URL,
'token': TOKEN,
'scheme': 'https',
'port': 443,
})
driver.login()
driver.posts.update_post(message_id, options={
'channel_id':channel,
'message': message
})
def delete_message_in_channel(message_id):
"""Delete message in channel.
:param message_id: Message id
:type message_id: str
"""
driver = Driver({
'url': URL,
'token': TOKEN,
'scheme': 'https',
'port': 443,
})
driver.login()
driver.posts.delete_post(message_id)