forked from trestoa/slack-to-telegram-bot
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathbot.py
More file actions
66 lines (51 loc) · 2.05 KB
/
bot.py
File metadata and controls
66 lines (51 loc) · 2.05 KB
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
import os
import sys
import time
import re
from slackclient import SlackClient
from telegram.bot import Bot
def format_slack(s):
txt_result = s
m = re.search(r'\<@([a-zA-Z0-9]*?)\>', s)
if m:
UID=m.groups()[0]
user = sc.api_call("users.info",user=UID)['user']
user_name=user['real_name']
txt_result = re.sub(r'\<@(.*?)\>', user_name, txt_result)
# ID: field removal
txt_result = re.sub(r'(\*ID\:\*\s[0-9]*\n)', '', txt_result)
# Due date format
txt_result = re.sub(r'\<\!(date\^.*?\})\|(.*?)\>', r'\2', txt_result)
# link to project/sub-task format
txt_result = re.sub(r'\<(https.*?)\|(.*?)\>', r'\2', txt_result)
# Removing bold formatting
txt_result = re.sub(r'\*(.*?)\*', r'\1', txt_result)
return txt_result
SLACK_TOKEN = os.environ['SLACK_TOKEN']
sc = SlackClient(SLACK_TOKEN)
TELEGRAM_TOKEN = os.environ['TELEGRAM_TOKEN']
TELEGRAM_TARGET = os.environ['TELEGRAM_TARGET']
telegram_bot = Bot(TELEGRAM_TOKEN)
VERBOSE_MODE = os.environ.get('VERBOSE', '0')
if VERBOSE_MODE=='1':
print("Verbose mode is enabled")
if sc.rtm_connect():
while True:
messages = sc.rtm_read()
for message in messages:
try:
if message['type'] == 'message':
if VERBOSE_MODE=='1':
print(message)
if (message['subtype'] == 'bot_message') and (message['bot_id'] == 'B7AMC8MPE'):
msg_string = ""
for attachement in message['attachments']:
text = format_slack(attachement['text'])
msg_string += "%s: %s (%s)" % (attachement['fallback'],text,attachement['title_link'])
telegram_bot.sendMessage(TELEGRAM_TARGET, msg_string)
except:
print('Could not send message.')
print("Unexpected error:", sys.exc_info()[0])
time.sleep(1)
else:
print("Connection Failed, invalid token?")