handlers.py 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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.HTTPSConnection('lcm.mi.infn.it:443')
  22. conn.request('HEAD', '/')
  23. s = conn.getresponse().status
  24. if s == 200:
  25. update.message.reply_text(text='Website is OK')
  26. update.message.reply_text(text='Web server replied with code %s ' % s)
  27. except StandardError:
  28. update.message.reply_text(text='An error occurred while connecting')
  29. def vietnam(update, context):
  30. """Spout wise words"""
  31. update.message.reply_text(text='Ricordate, ragazzi, LCM è come il Vietnam. '
  32. 'Una volta entrati, è impossibile uscirne!',
  33. quote=False)
  34. def sell_your_mother(update, context):
  35. """Remind people not to disclose passwords"""
  36. pic_id = 'AgADBAADz7AxG0u34VPYhr3TqmbSck56qBsABAEAAwIAA3gAA_ZiBQABFgQ'
  37. update.message.reply_photo(photo=pic_id, caption='cit. Mandelli',
  38. quote=False)
  39. def print_msg_info(update, context):
  40. """Print all message info to console - useful for debugging purposes"""
  41. print update.message
  42. def error(update, context, error):
  43. """Log errors"""
  44. # create a logger with function scope ("static" object)
  45. error.logger = logging.getLogger(__name__)
  46. error.logger.warn('Update "%s" caused error "%s"' % (update, error))
  47. def abuse_150(update, context):
  48. """Verbally abuse incompetent LCM collaborators"""
  49. insult = np.random.choice(insults)
  50. update.message.reply_text(text=insult, quote=False)
  51. def moarpuddu(update,context):
  52. """More broken pipes for Puddu"""
  53. update.message.reply_text(text="più broken pipe per Puddu", quote=False)
  54. def speak(update, context, *args):
  55. """Produce pseudo-random wise words"""
  56. word = args[0] if len(args) > 0 else None
  57. update.message.reply_text(text=produce_sentence(word), quote=False)