22import hmac
33import json
44import logging
5+ import os
56import sys
67import time
78import urllib .parse
8- import os
9+ from sqlite3 import connect , cursor , execute
910
1011import requests
1112from flask import Flask , request
@@ -66,6 +67,32 @@ def publish_message(body, action):
6667 )
6768
6869
70+ async def get_messages ():
71+ conversation_list = requests .get ("https://slack.com/api/conversations.list" )
72+ channel_id = conversation_list .json ()["channels" ]["abaquery" ]
73+ message_history = requests .get (
74+ "https://slack.com/api/conversations.history" , {id : channel_id }
75+ )
76+ messages = message_history .json ()["messages" ]
77+
78+ return messages
79+
80+
81+ async def backup_message (body , action ):
82+ messages = await get_messages ()
83+ try :
84+ con = connect ("backup.db" )
85+ cur = con .cursor ()
86+ except Exception as e :
87+ print (f"Exception initializing database: { str (e )} " )
88+ logging .exception ("Error Database" )
89+
90+ cur .execute (
91+ "CREATE TABLE IF NOT EXISTS MELDINGER (id INTEGER PRIMARY KEY, message TEXT, thread VARCHAR(16));"
92+ )
93+ cur .executemany ("INSERT INTO MELDINGER (id, message, thread) VALUES (?,?)" , [(message .text , message .thread_ts ) for message in messages ])
94+ #Need to sanitize somehow
95+
6996def handle_action (body ):
7097 action = body ["callback_id" ]
7198 print (f'Handling { body ["type" ]} request. Action is: { action } ' )
@@ -90,6 +117,9 @@ def handle_action(body):
90117 open_dialog (body , action )
91118 elif body ["type" ] == "dialog_submission" :
92119 publish_message (body , action )
120+ elif body ["type" ] == "backup" :
121+ backup_message (body , action )
122+ #Need to add the action somewhere
93123
94124
95125def open_dialog (body , action ):
0 commit comments