Skip to main content

The Basis Poster for the matrix-sm-poster.

Project description

Matrix Social Media Poster Base Poster

Author: polyma3000

How to set up

Install this package

pip install matrix-sm-poster-basis-poster

Setup connections

To access the Platform connections, we need to create a config file.

Let's create connections.yaml:

mastodon_connections:
  connection_name:
    domain: "https://your.domain"
    token: "your_token"

To get the credentials just go to your settings and under the developer menu there is a place to create a new app and get your credentials all in one go.

Visit https://tinysubversions.com/notes/mastodon-bot/ if the version of your server has an old version which doesn't allow you to get credentials.

For other platforms use the name of the platform and add _connections in the top hierarchy of your connections.yaml

Derive Message Class

At first, you have to create an own Message class derived from basis_poster.Message and overload the async method send_to_platform. This example is based on mastodon.py.

Let's create MyMessage.py:

from basis_poster import Message
from mastodon import Mastodon
from mastodon.Mastodon import MastodonNetworkError
from urllib3.exceptions import ConnectTimeoutError, MaxRetryError


class MyMessage(Message):
    async def send_to_platform(self, platform_connection: Mastodon):
        pictures = []
        if self.pictures_names:
            for picture_name in self.pictures_names:
                pictures.append(
                    platform_connection.media_post(media_file=picture_name))

        if pictures:
            posted_posts = [platform_connection.status_post(
                status=self.posts.pop(0), media_ids=pictures)]
        else:
            posted_posts = [platform_connection.status_post(
                status=self.posts.pop(0))]

        while self.posts:
            posted_posts.append(platform_connection.status_post(status=self.posts.pop(0),
                                                                in_reply_to_id=posted_posts[len(posted_posts) - 1]))
    
    @classmethod
    def get_errors(cls):
        return TimeoutError, ConnectTimeoutError, MaxRetryError, MastodonNetworkError

In self.pictures_names the filenames of the pictures are listed. In self.posts the posts to send are available.

Derive PlatformHandler Class

At first, you have to create an own PlatformHandler class derived from basis_poster.PlatformHandler and overload the static method add_platform_connection. This example is based on mastodon.py.

Let's create MyPlatformHandler.py:

from basis_poster import PlatformHandler
from mastodon import Mastodon


class MyPlatformHandler(PlatformHandler):
    @staticmethod
    def add_platform_connection(platform_connection_name: str, platform_connection: dict):
        return Mastodon(
                access_token=platform_connection['token'],
                api_base_url=platform_connection['domain'],
            )

platform_connection hands over the data stored for this connection in connections.yaml.

Use Platform Handler

To start your Platform we need to set up the PlatformHandler and start it.

Let's create main.py:

from MyMessage import MyMessage
from MyPlatformHandler import MyPlatformHandler


import logging

def main():
    logging.getLogger().debug('Started..')
    
    handler = MyPlatformHandler(
        MyMessage,
        'mastodon',
        500,
        db_filename="shared_messages.db",
        connections_filename="connections.yaml",
        pictures_directory_name="pictures"
    )
    
    handler.run()
    
    logging.getLogger().debug('Finished..')

if __name__ == '__main__':
    main()

db_filename ist the path to the sqlite database, where the PlatformHandler gets the messages to send from. connections_filename ist the path to the connections config we set up above. pictures_directory_name ist the path to the pictures to send with messages.

How to set up the database

CREATE TABLE IF NOT EXISTS messages (
id INTEGER NOT NULL PRIMARY KEY,
matrix_connection_name VARCHAR(100), 
matrix_room_id VARCHAR(100), 
body Text,
pictures_ids TEXT,
created_datetime DATETIME,
sent BOOLEAN DEFAULT 0
);

CREATE TABLE IF NOT EXISTS messages_to_platforms (
id INTEGER NOT NULL PRIMARY KEY,
messages_id INTEGER NOT NULL,
platform_name VARCHAR(100), 
json_data TEXT, 
sent BOOLEAN DEFAULT 0
);

PRAGMA journal_mode = WAL;

Possible simple connections

Project details


Release history Release notifications | RSS feed

This version

0.2

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

matrix-sm-poster-basis-poster-0.2.tar.gz (8.8 kB view details)

Uploaded Source

Built Distribution

File details

Details for the file matrix-sm-poster-basis-poster-0.2.tar.gz.

File metadata

File hashes

Hashes for matrix-sm-poster-basis-poster-0.2.tar.gz
Algorithm Hash digest
SHA256 e94f5bd3d59d5582a15c61830d2d5cd24765e15076392b7bd4663b7873b31d42
MD5 1b8ddf6fec8480fa97c7f54494a2baee
BLAKE2b-256 698d020aed8bca4175ac7493cc29a5dcb91779ea6b78a8a6226b39f4c2e390fa

See more details on using hashes here.

File details

Details for the file matrix_sm_poster_basis_poster-0.2-py3-none-any.whl.

File metadata

File hashes

Hashes for matrix_sm_poster_basis_poster-0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 143cae3eb8c8ed029c39a474b7679bdd409c98a28e5da8c9693d9042468a5bdf
MD5 9d5c8b36f6cf738c355398e728c51fe6
BLAKE2b-256 734e91a6c59b4407df016c94f55afcd235061acec3a804971c493f5ed95325bd

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page