MyTelegramBot.py 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. from YoConfig import YoConfig
  2. import redis
  3. from random import randint
  4. from telegram.ext import Updater, CommandHandler, CallbackQueryHandler, MessageHandler, Filters
  5. from telegram import InlineKeyboardMarkup, InlineKeyboardButton
  6. import telegram
  7. import datetime
  8. """ import logging
  9. logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
  10. level=logging.INFO) """
  11. """ def start(update, context):
  12. context.bot.send_message(chat_id=update.effective_chat.id, text="I'm a bot, please talk to me!") """
  13. r = redis.Redis(host=YoConfig['db']['choozmo_redis']['REDIS_HOST'], port=YoConfig['db']['choozmo_redis']['REDIS_PORT'], password=YoConfig['db']['choozmo_redis']['REDIS_PASSWORD'], db=1)
  14. def start(update, bot):
  15. a, b = randint(1, 100), randint(1, 100)
  16. update.message.reply_text('{} + {} = ?'.format(a, b),
  17. reply_markup = InlineKeyboardMarkup([[
  18. InlineKeyboardButton(str(s), callback_data = '{} {} {}'.format(a, b, s)) for s in range(a + b - randint(1, 3), a + b + randint(1, 3))
  19. ]]))
  20. def answer(update, bot):
  21. a, b, s = [int(x) for x in update.callback_query.data.split()]
  22. if a + b == s:
  23. update.callback_query.edit_message_text('你答對了!')
  24. else:
  25. update.callback_query.edit_message_text('你答錯囉!')
  26. updater = Updater('1773608337:AAF0TVRKXQu4vB3ii7JkM5kuV2vsXkvX7ss', use_context=True)
  27. j = updater.job_queue
  28. dispatcher = updater.dispatcher
  29. """ dispatcher.add_handler(CommandHandler('start', start))
  30. dispatcher.add_handler(CallbackQueryHandler(answer)) """
  31. def echo(update, context):
  32. context.bot.send_message(chat_id=update.effective_chat.id, text=update.message.text)
  33. echo_handler = MessageHandler(Filters.text & (~Filters.command), echo)
  34. dispatcher.add_handler(echo_handler)
  35. def caps(update, context):
  36. text_caps = ' '.join(context.args).upper()
  37. context.bot.send_message(chat_id=update.effective_chat.id, text=text_caps)
  38. caps_handler = CommandHandler('caps', caps)
  39. dispatcher.add_handler(caps_handler)
  40. def callback_alarm(context):
  41. context.bot.send_message(chat_id=context.job.context, text='BEEP')
  42. def callback_timer(update, context):
  43. context.bot.send_message(chat_id=update.message.chat_id,
  44. text='Setting a timer for 1 minute!')
  45. context.job_queue.run_repeating(callback_alarm, 60, context=update.message.chat_id)
  46. timer_handler = CommandHandler('timer', callback_timer)
  47. dispatcher.add_handler(timer_handler)
  48. def callback_minute(context: telegram.ext.CallbackContext):
  49. context.bot.send_message(chat_id='1053416326',
  50. text='Im online.')
  51. context.bot.send_message(chat_id='-1001177266889',
  52. text='Im online.')
  53. updater.job_queue.run_once(callback_minute, 3)
  54. def hhh_space_monitor(context: telegram.ext.CallbackContext):
  55. context.bot.send_message(chat_id='-1001177266889',
  56. text='this is space monitor. space_hhhapi: \n' + str(r.get('space_hhhapi').decode('utf-8')))
  57. updater.job_queue.run_daily(hhh_space_monitor, datetime.time(1, 00, 00))
  58. updater.job_queue.run_once(hhh_space_monitor, 3)
  59. updater.start_polling()
  60. updater.idle()