No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

jabbergram.py 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. import sleekxmpp
  4. import telegram
  5. import logging
  6. import configparser
  7. from threading import Thread
  8. from queue import Queue
  9. from telegram.error import NetworkError, Unauthorized
  10. from time import sleep
  11. from sys import argv
  12. from sys import exit
  13. class EchoBot(sleekxmpp.ClientXMPP):
  14. def __init__(self, jid, password, room, nick, token, group):
  15. # XMPP
  16. super(EchoBot, self).__init__(jid, password)
  17. self.add_event_handler('session_start', self.start)
  18. self.add_event_handler('groupchat_message', self.muc_message)
  19. self.muc_room = room
  20. self.nick = nick
  21. self.token = token
  22. # Telegram
  23. self.group = group
  24. self.bot = telegram.Bot(self.token)
  25. # meter el conecto del tg en un hilo
  26. t = Thread(target=self.read_tg)
  27. t.daemon = True
  28. t.start()
  29. print('Please wait a couple of minutes until it\'s correctly connected'
  30. ' to telegram.')
  31. while True:
  32. out = input('Enter "quit" if you want to stop the program.\n')
  33. if out == 'quit':
  34. exit()
  35. def read_tg(self):
  36. update_id = 0
  37. while True:
  38. try:
  39. for update in self.bot.getUpdates(offset=update_id,
  40. timeout=10):
  41. message = update.message.text
  42. user = str(update.message.from_user.username)
  43. if not user:
  44. user = str(update.message.from_user.first_name)
  45. mensaje = user + ": " + message
  46. chat_id = update.message.chat_id
  47. if message and chat_id == self.group:
  48. self.send_message(mto=self.muc_room,
  49. mbody=mensaje,
  50. mtype='groupchat')
  51. update_id = update.update_id + 1
  52. except NetworkError:
  53. sleep(1)
  54. except Unauthorized:
  55. sleep(1)
  56. def start(self, event):
  57. self.get_roster()
  58. self.send_presence()
  59. self.plugin['xep_0045'].joinMUC(self.muc_room, self.nick, wait=True)
  60. def muc_message(self, msg):
  61. if msg['mucnick'] != self.nick:
  62. mensaje = str(msg['from']).split('/')[1] + ': ' + str(msg['body'])
  63. self.bot.sendMessage(self.group, text=mensaje)
  64. if __name__ == '__main__':
  65. # parsear config
  66. config = []
  67. parser = configparser.SafeConfigParser()
  68. if len(argv) == 2:
  69. parser.read(argv[1])
  70. else:
  71. parser.read('config.ini')
  72. for name, value in parser.items('config'):
  73. config.append(value)
  74. # asignar valores para el bot
  75. jid = config[0]
  76. password = config[1]
  77. muc_room = config[2]
  78. nick = config[3]
  79. token = config[4]
  80. group = int(config[5])
  81. xmpp = EchoBot(jid, password, muc_room, nick, token, group)
  82. xmpp.register_plugin('xep_0045')
  83. if xmpp.connect():
  84. xmpp.process(block=True)
  85. print("Done")
  86. else:
  87. print("Unable to connect.")
  88. # Vols un gram nen?