tg-sync

git clone git://archive.git.mtrnord.blog/Nordgedanken/tg-sync.git
Log | Files | Refs | README

Hslack.py (2289B)


      1 import logging, time
      2 
      3 logger = logging.getLogger(__name__)
      4 
      5 from bot.helper import sc, tg_bot, memory_class
      6 
      7 class Slack:
      8     def __init__(self):
      9         self.sc = sc
     10         self.tg_bot = tg_bot
     11         self.memory = memory_class
     12 
     13     def slack_init(self):
     14         try:
     15             sl2tg = self.memory.get_by_path(['tg_sl-sync'])['sl2tg']
     16             logger.info("Slack Bot starting...")
     17             if self.sc.rtm_connect():
     18                 last_ping = int(time.time())
     19                 while True:
     20                     rtm_flow = self.sc.rtm_read()
     21                     if rtm_flow:
     22                         print(rtm_flow)
     23                         type = rtm_flow[0]['type']
     24                         if 'text' in rtm_flow[0]:
     25                             if type == "message":
     26                                     if "subtype" in rtm_flow[0]:
     27                                         subtype = rtm_flow[0]['subtype']
     28                                         if "message_deleted" in subtype:
     29                                             logger.debug("something got deleted at slack")
     30                                     else:
     31                                         text = rtm_flow[0]['text']
     32                                         user_profile = self.sc.api_call("users.info", user=rtm_flow[0]['user'])
     33                                         channel_info = self.sc.api_call("channels.info", channel=rtm_flow[0]['channel'])
     34                                         channel_name = channel_info['channel']['name']
     35                                         user_name = user_profile['user']['profile']['real_name']
     36                                         response = self.tg_bot.sendMessage(parse_mode='HTML', chat_id=sl2tg['#{}'.format(channel_name)], text='<b>{user_name} ({channel}):</b> {text}'.format(user_name=user_name, channel=channel_name, text=text))
     37                             else:
     38                                 logger.debug(rtm_flow)
     39                     now = int(time.time())
     40                     if now > last_ping + 30:
     41                         self.sc.server.ping()
     42                         last_ping = now
     43                     time.sleep(.1)
     44             else:
     45                 logger.critical("Connection Failed, invalid token?")
     46         except KeyboardInterrupt:
     47             sys.exit()