forked from trestoa/slack-to-telegram-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.py
43 lines (37 loc) · 1.46 KB
/
bot.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
import os
import sys
import time
from slackclient import SlackClient
from telegram.bot import Bot
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)
if sc.rtm_connect():
while True:
messages = sc.rtm_read()
for message in messages:
try:
if message['type'] == 'message':
if message['channel'][0] == 'G':
channel = sc.api_call(
"groups.info",
channel=message['channel'])['group']
elif message['channel'][0] == 'C':
channel = sc.api_call(
"channels.info",
channel=message['channel'])['channel']
else:
channel = {'name': 'bot'}
user = sc.api_call(
"users.info",
user=message['user'])['user']
msg_string = '@{} posted to #{}: {}'.format(user['name'], channel['name'], message['text'])
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?")