handlers.py 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. import logging
  8. # enable logging
  9. fmt = '%(asctime)s - %(name)s - %(levelname)s - %(message)s'
  10. logging.basicConfig(format=fmt, level=logging.INFO)
  11. def ping(bot, update):
  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(bot, update):
  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(bot, update):
  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(bot, update):
  33. """Remind people not to disclose passwords"""
  34. pic_id = 'AgADBAADbasxG9JPlAQNlEW3ML5sk_bEXxkABHKAFZ1ZzBZsNvMBAAEC'
  35. update.message.reply_photo(photo=pic_id, caption='cit. Mandelli',
  36. quote=False)
  37. def print_msg_info(bot, update):
  38. """Print all message info to console - useful for debugging purposes"""
  39. print update.message
  40. def error(bot, update, 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(bot, update):
  46. """Verbally abuse incompetent LCM collaborators"""
  47. insult = np.random.choice(insults)
  48. update.message.reply_text(text=insult, quote=False)
  49. def speak(bot, update, args):
  50. """Produce pseudo-random wise words"""
  51. word = args[0] if len(args) > 0 else None
  52. update.message.reply_text(text=produce_sentence(word), quote=False)