2022-10-07 11:29:18 +00:00
import mattermostdriver . exceptions
2024-03-26 20:52:18 +00:00
import mattermost_client
2024-03-26 23:00:54 +00:00
from config import config
2024-03-27 17:03:18 +00:00
from mattermost_client import ChannelApi , MMApi
2024-03-26 23:00:54 +00:00
2022-10-07 11:29:18 +00:00
def send_message ( file_id , file_info , message ) :
channel_id = file_info [ " originating_mm_post_channel_id " ]
post_id = file_info [ " originating_mm_post_id " ]
# TODO Comment below line, this is for testing purposes
# channel_id = MMApi().get_channel_id("bestuur-dev")
channel = ChannelApi (
channel_id = channel_id ,
2024-03-27 17:02:53 +00:00
user = mattermost_client . users [ config [ " mattermost " ] [ " selected_user " ] ] ,
2022-10-07 11:29:18 +00:00
)
prefix = " "
# This is bestuur-INTERN where you can only post when you prefix you message with a '!'
if file_info [ " originating_mm_post_channel_id " ] == " hrx6pgfswjbttcj8nim3jrwe7w " :
prefix = " ! "
try :
channel . create_threaded_post (
post_id ,
f " { prefix } { message } " ,
)
except mattermostdriver . exceptions . InvalidOrMissingParameters as e :
# This will occur when we try to react to a file in a channel that is not the same as the originating channel.
2024-03-26 23:00:54 +00:00
unique_post_url = f " { config [ ' mattermost ' ] [ ' server_url ' ] } /pl/ { post_id } "
2022-10-07 11:29:18 +00:00
channel . create_post (
f " { unique_post_url } \n \n { message } " ,
)
def report_newly_found_file ( file_id , file_info ) :
2024-03-27 16:16:34 +00:00
git_url = f " https:// { config [ ' gitea ' ] [ ' server_url ' ] } / { config [ ' gitea ' ] [ ' remote_org ' ] } / { config [ ' gitea ' ] [ ' remote_repo ' ] } "
message = f " I found a new CodiMD file in this post! Making work of putting it on git :) \n - Requested location in the [drive]( { git_url } ): { file_info [ ' metadata ' ] [ ' sync-to ' ] } "
2022-10-07 11:29:18 +00:00
send_message ( file_id , file_info , message )
def report_newly_found_but_invalid_file ( file_id , file_info ) :
message = """ Hi there! :wave:
I ' m your friendly neighbourhood document sync bot.
2024-03-27 17:07:34 +00:00
I could synchronize this CodiMD file automatically to our Git DRIVE for safekeeping , but the necessary metadata block is not present .
2022-10-07 11:29:18 +00:00
You can easily add the correct info and I will do the rest of the work for you !
Just add the following lines to your file , the location in your file is not important but at the top would be my recommendation .
` ` `
2024-03-26 23:00:54 +00:00
: : : spoiler git drive sync
2022-10-07 11:29:18 +00:00
- sync - to : < a valid path on the DRIVE , for ex . : verslagen / 21 - 22 / 2022 - 05 - 13. md >
: : :
` ` ` """
send_message ( file_id , file_info , message )