bot.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. #!/usr/bin/env python
  2. import os
  3. import logging
  4. from tempfile import mkstemp
  5. from telegram.ext import Updater, CommandHandler, MessageHandler, Filters, \
  6. InlineQueryHandler
  7. from telegram import InlineQueryResultVoice, InlineQueryResultAudio
  8. from telegram.ext.dispatcher import run_async
  9. from translate import get_audio_url, get_audio
  10. from config import token
  11. logging.basicConfig(
  12. format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
  13. level=logging.INFO)
  14. logger = logging.getLogger(__name__)
  15. @run_async
  16. def onstart(bot, update):
  17. logger.info('start command from %s' % update.message.from_user.first_name)
  18. bot.send_message(chat_id=update.message.chat_id, text='Hello! I\'m R2D2.')
  19. onhelp(bot, update)
  20. @run_async
  21. def onhelp(bot, update):
  22. logger.info('help command from %s' % update.message.from_user.first_name)
  23. bot.send_message(chat_id=update.message.chat_id, text='''
  24. Type "@artoobot <message>" to get inline voice.
  25. Commands:
  26. /help Show this help message.
  27. /say <message> Reply with a translated message (use to get voice next to \
  28. original text.)
  29. Messages downloaded with the download button are saved to your downloads \
  30. folder! Make sure to clear them if used and to use `voice' messages whenever \
  31. possible.
  32. '''.strip())
  33. def reply_audio(bot, chat_id, text, reply_id):
  34. fd, path = mkstemp()
  35. f = os.fdopen(fd, 'w+b')
  36. f.write(get_audio(text))
  37. f.flush()
  38. f.seek(0)
  39. bot.send_voice(chat_id=chat_id, voice=f, reply_to_message_id=reply_id)
  40. f.close()
  41. os.unlink(path)
  42. @run_async
  43. def onsay(bot, update, args):
  44. m = update.message
  45. text = ' '.join(args)
  46. logger.info('say command from %s: %s' % (m.from_user.first_name, text))
  47. reply_audio(bot, m.chat_id, text, m.message_id)
  48. @run_async
  49. def onmessage(bot, update, reply=False):
  50. m = update.message
  51. logger.info('message from %s: %s' % (m.from_user.first_name, m.text))
  52. reply_audio(bot, m.chat_id, m.text, None)
  53. @run_async
  54. def oninline(bot, update):
  55. q = update.inline_query.query
  56. logger.info('inline query from %s: %s' %
  57. (update.inline_query.from_user.first_name, q))
  58. if not q:
  59. return
  60. url = get_audio_url(q)
  61. results = [
  62. InlineQueryResultAudio(id='audio_%s' % q, audio_url=url,
  63. title='Audio (download button, shows text)'),
  64. InlineQueryResultVoice(id='voice_%s' % q, voice_url=url,
  65. title='Voice (play button, no text)')
  66. ]
  67. bot.answer_inline_query(update.inline_query.id, results)
  68. @run_async
  69. def onunknown(bot, update):
  70. logger.info('unknown command from %s' %
  71. update.message.from_user.first_name)
  72. bot.send_message(chat_id=update.message.chat_id,
  73. text='Sorry, I didn\'t understand that command.')
  74. def onerror(bot, update, error):
  75. logger.warn('Update "%s" caused error "%s"' % (update, error))
  76. if __name__ == '__main__':
  77. updater = Updater(token=token)
  78. dp = updater.dispatcher
  79. dp.add_handler(CommandHandler('start', onstart))
  80. dp.add_handler(CommandHandler('help', onhelp))
  81. dp.add_handler(CommandHandler('say', onsay, pass_args=True))
  82. dp.add_handler(InlineQueryHandler(oninline))
  83. dp.add_handler(MessageHandler([Filters.command], onunknown))
  84. dp.add_handler(MessageHandler([Filters.text], onmessage))
  85. dp.add_error_handler(onerror)
  86. updater.start_polling()
  87. updater.idle()