Skip to content

Commit 4888aca

Browse files
committed
Draft for adding backup functionality
1 parent e29cb11 commit 4888aca

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

req.txt renamed to requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ raven
22
flask
33
gevent
44
requests
5+
sqlite3

server.py

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22
import hmac
33
import json
44
import logging
5+
import os
56
import sys
67
import time
78
import urllib.parse
8-
import os
9+
from sqlite3 import connect, cursor, execute
910

1011
import requests
1112
from 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+
6996
def 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

95125
def open_dialog(body, action):

0 commit comments

Comments
 (0)