forked from cmyr/debt-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
slask.py
63 lines (52 loc) · 1.55 KB
/
slask.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
from glob import glob
import importlib
import json
import os
import re
import sys
import traceback
import urllib
from flask import Flask, request
app = Flask(__name__)
curdir = os.path.dirname(os.path.abspath(__file__))
os.chdir(curdir)
import debt_utils
from config import config
def handle_message(message):
# 'help'
if re.findall(r"^help", message.get("text", ""), flags=re.IGNORECASE):
return debt_utils.help_message()
# 'status'
if re.findall(r"^status", message.get("text", ""), flags=re.IGNORECASE):
return debt_utils.status_for_user(message.get("user_id"))
# 'transactions'
if re.findall(r"^transactions", message.get("text", ""), flags=re.IGNORECASE):
transactions = debt_utils.transactions(message.get("user_id"))
if len(transactions):
return "\n".join([urllib.unquote(m.get("text", ""))
for m in transactions])
return None
@app.route("/", methods=['POST'])
def main():
username = config.get("username", "slask")
icon = config.get("icon", ":poop:")
# ignore message we sent
msguser = request.form.get("user_name", "").strip()
if username == msguser or msguser.lower() == "slackbot":
return ""
text = None
try:
text = handle_message(request.form)
except Exception as err:
text = str(err)
if not text:
return ""
response = {
"text": text,
"username": username,
"icon_emoji": icon,
"parse": "full",
}
return json.dumps(response)
if __name__ == "__main__":
app.run()