Package: matrix-sm-poster-basis-poster
Matrix Social Media Poster Base PosterAuthor: polyma3000How to set upInstall this packagepipinstallmatrix-sm-poster-basis-posterSetup connectionsTo access the Platform connections, we need to create a config file.Let's createconnections.yaml:mastodon_connections:connection_name:domain:"https://your.domain"token:"your_token"To get the credentials just go to your settings and under thedevelopermenu there is a place to create a new app and get your credentials all in one go.Visithttps://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_connectionsin the top hierarchy of yourconnections.yamlDerive Message ClassAt 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 onmastodon.py.Let's createMyMessage.py:frombasis_posterimportMessagefrommastodonimportMastodonfrommastodon.MastodonimportMastodonNetworkErrorfromurllib3.exceptionsimportConnectTimeoutError,MaxRetryErrorclassMyMessage(Message):asyncdefsend_to_platform(self,platform_connection:Mastodon):pictures=[]ifself.pictures_names:forpicture_nameinself.pictures_names:pictures.append(platform_connection.media_post(media_file=picture_name))ifpictures: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))]whileself.posts:posted_posts.append(platform_connection.status_post(status=self.posts.pop(0),in_reply_to_id=posted_posts[len(posted_posts)-1]))@classmethoddefget_errors(cls):returnTimeoutError,ConnectTimeoutError,MaxRetryError,MastodonNetworkErrorInself.pictures_namesthe filenames of the pictures are listed.
Inself.poststhe posts to send are available.Derive PlatformHandler ClassAt first, you have to create an ownPlatformHandlerclass derived frombasis_poster.PlatformHandlerand overload the static methodadd_platform_connection.
This example is based onmastodon.py.Let's createMyPlatformHandler.py:frombasis_posterimportPlatformHandlerfrommastodonimportMastodonclassMyPlatformHandler(PlatformHandler):@staticmethoddefadd_platform_connection(platform_connection_name:str,platform_connection:dict):returnMastodon(access_token=platform_connection['token'],api_base_url=platform_connection['domain'],)platform_connectionhands over the data stored for this connection inconnections.yaml.Use Platform HandlerTo start your Platform we need to set up the PlatformHandler and start it.Let's createmain.py:fromMyMessageimportMyMessagefromMyPlatformHandlerimportMyPlatformHandlerimportloggingdefmain():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_filenameist the path to the sqlite database, where thePlatformHandlergets the messages to send from.connections_filenameist the path to the connections config we set up above.pictures_directory_nameist the path to the pictures to send with messages.How to set up the databaseCREATETABLEIFNOTEXISTSmessages(idINTEGERNOTNULLPRIMARYKEY,matrix_connection_nameVARCHAR(100),matrix_room_idVARCHAR(100),bodyText,pictures_idsTEXT,created_datetimeDATETIME,sentBOOLEANDEFAULT0);CREATETABLEIFNOTEXISTSmessages_to_platforms(idINTEGERNOTNULLPRIMARYKEY,messages_idINTEGERNOTNULL,platform_nameVARCHAR(100),json_dataTEXT,sentBOOLEANDEFAULT0);PRAGMAjournal_mode=WAL;Possible simple connectionshttps://github.com/halcy/Mastodon.pyhttps://github.com/python-telegram-bot/python-telegram-bot