-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
69 lines (62 loc) · 1.97 KB
/
main.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
64
65
66
67
68
69
import os
import requests
import logging
import yaml
from slack_bolt import App
from slack_bolt.adapter.socket_mode import SocketModeHandler
from slack_sdk import WebClient
# TODO: Add command line arguments parsing
# Uncomment next line for debugging
#logging.basicConfig(level=logging.INFO)
channels = {}
users = {}
with open("routing.conf", "r") as f:
routing = yaml.safe_load(f)
logging.info ("Routing is loaded: %s", routing)
# Initializes your app with your bot token and socket mode handler
app = App(token=os.environ.get("SLACK_BOT_TOKEN"))
client = WebClient(token=os.environ.get("SLACK_BOT_TOKEN"))
tgtoken = os.environ.get("TG_BOT_TOKEN")
@app.event("message")
def handle_message_events(message):
# Find and cache real user name
try:
users[message['user']]
except KeyError:
user = client.users_info(
user=message['user']
)
user_name = user['user']['real_name']
users[message['user']] = user_name
logging.info ("Added %s user to cache", user_name)
else:
user_name = users[message['user']]
# Find and cache real channel name
try:
channels[message['channel']]
except KeyError:
channel = client.conversations_info(
channel=message['channel']
)
channel_name = channel['channel']['name']
channels[message['channel']] = channel_name
logging.info ("Added %s channel to cache", channel_name)
else:
channel_name = channels[message['channel']]
# Find if routing is defined, otherwise drop the message
try:
routing[channel_name]
except KeyError:
logging.info ('No route for %s', channel_name)
else:
logging.info ('%s => %s', message['channel'], channel_name)
chat_id = routing[channel_name]
text = f"{user_name}: {message['text']}"
url = f"https://api.telegram.org/bot{tgtoken}/sendMessage?chat_id={chat_id}&text={text}"
r = requests.get(url)
status = r.json()
if status['ok'] != True:
logging.warning ('%s', status)
# Start the app
if __name__ == "__main__":
SocketModeHandler(app, os.environ["SLACK_APP_TOKEN"]).start()