Python library to use Mattermost APIv4.
This repository is a fork of Python Mattermost Driver.
- Python 3.10 or greater
poetry
As this project is not published to Pypi yet, follow the guidelines below.
-
Clone the repository and change it to your working directory.
-
Install the project:
$ poetry install
- Activate the virtual environment:
$ source `poetry env info --path`/bin/activate
- Use it as a library.
Example usage in the
examples
folder.
A Driver, or client, is an object used to interact with the Mattermost API.
At least the following options must be provided as a dict:
login_id
(user account's email address or username) andpassword
- or
token
Full list of Driver options here.
Example with synchronous driver:
from requests import ConnectionError
from scrapermost import AsyncDriver
from scrapermost.exceptions import NoAccessTokenProvided
def init_driver(server_host: str, email: str, password: str) -> Driver:
return Driver(
{
"hostname": server_host,
"login_id": email,
"password": password,
"scheme": "https",
"port": 443,
}
)
def connect_driver_to_server(driver: Driver) -> None:
try:
driver.login()
except (ConnectionError, NoAccessTokenProvided) as err:
print(f"Driver login failed: {err}")
Example with asynchronous driver:
from requests import ConnectionError
from scrapermost import AsyncDriver
from scrapermost.exceptions import NoAccessTokenProvided
def init_driver(server_host: str, email: str, password: str) -> AsyncDriver:
return AsyncDriver(
{
"hostname": server_host,
"login_id": email,
"password": password,
"scheme": "https",
"port": 443,
}
)
async def connect_driver_to_server(driver: AsyncDriver) -> None:
try:
await driver.login()
except (ConnectionError, NoAccessTokenProvided) as err:
print(f"Driver login failed: {err}")
You can make api calls by using calling Driver.endpointofchoice
. For example, if you want to get a user's data (http://your-mattermost-url.com/api/v4/users/{user_id}
), you would use Driver.users.get_user(user_id)
. The returned data will be either in JSON format or the raw response.
Example with asynchronous driver:
from typing import Any
response: Any = await driver.users.get_user(user_id="me")
It is possible to use a websocket to listen to Mattermost events (event list here).
Create a function to handle every Mattermost websocket event:
from typing import Any, Dict
from scrapermost.events import Posted
# Minimalist event handler example
async def handle_new_post(event: Dict[str, Any]) -> None:
if event.get("event") == "posted":
post: Posted = Posted(event)
...
Assuming Driver.login()
was called, connect the websocket to the Mattermost server using Driver.start_websocket()
.
Example with synchronous driver:
driver.start_websocket(handle_new_post)
Example with asynchronous driver:
await driver.start_websocket(handle_new_post)
Example with asynchronous driver:
await driver.disconnect_websocket()
await driver.logout()
Distributed under the MIT License. See LICENSE
for more information.
Original project Python Mattermost Driver (documentation here) by Christian Plümer.