handlers.py 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. # -*- coding: utf-8 -*-
  2. import subprocess as sp
  3. import httplib
  4. import numpy as np
  5. from insults import insults
  6. from speak import produce_sentence
  7. def start(update, context):
  8. """Says hi to new users"""
  9. #context.bot.send_message(chat_id=update.effective_chat.id, text="I'm a bot, please talk to me!")
  10. update.message.reply_text(text='Bot ufficiale di LCM')
  11. def ping(update, context):
  12. """Check LCM ping response"""
  13. try:
  14. sp.check_output(['ping', '-c', '1', 'lcm.mi.infn.it'])
  15. except CalledProcessError, e:
  16. update.message.reply_text(text='LCM is unreachable')
  17. update.message.reply_text(text='LCM is reachable')
  18. def is_web_up(update, context):
  19. """Check LCM http response"""
  20. try:
  21. conn = httplib.HTTPConnection('lcm.mi.infn.it:443')
  22. conn.request('HEAD', '/')
  23. s = conn.getresponse().status
  24. update.message.reply_text(text='Web server replied with code %s ' % s)
  25. except StandardError:
  26. update.message.reply_text(text='An error occurred while connecting')
  27. def vietnam(update, context):
  28. """Spout wise words"""
  29. update.message.reply_text(text='Ricordate, ragazzi, LCM è come il Vietnam. '
  30. 'Una volta entrati, è impossibile uscirne!',
  31. quote=False)
  32. def sell_your_mother(update, context):
  33. """Remind people not to disclose passwords"""
  34. pic_id = 'AgADBAAD4LExG98F2FMjGW6Jx6z07T2OtxsABAEAAwIAA3gAA6eOAAIWBA'
  35. update.message.reply_photo(photo=pic_id, caption='cit. Mandelli',
  36. quote=False)
  37. def print_msg_info(update, context):
  38. """Print all message info to console - useful for debugging purposes"""
  39. print update.message
  40. def error(update, context, error):
  41. """Log errors"""
  42. # create a logger with function scope ("static" object)
  43. error.logger = logging.getLogger(__name__)
  44. error.logger.warn('Update "%s" caused error "%s"' % (update, error))
  45. def abuse_150(update, context):
  46. """Verbally abuse incompetent LCM collaborators"""
  47. insult = np.random.choice(insults)
  48. update.message.reply_text(text=insult, quote=False)
  49. def moarpuddu(bot, update):
  50. """More broken pipes for Puddu"""
  51. update.message.reply_text(text="più broken pipe per Puddu", quote=False)
  52. def speak(update, context, *args):
  53. """Produce pseudo-random wise words"""
  54. word = args[0] if len(args) > 0 else None
  55. update.message.reply_text(text=produce_sentence(word), quote=False)