handlers.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. # -*- coding: utf-8 -*-
  2. import subprocess as sp
  3. import httplib
  4. import numpy as np
  5. from insults import insults
  6. import logging
  7. # enable logging
  8. fmt = '%(asctime)s - %(name)s - %(levelname)s - %(message)s'
  9. logging.basicConfig(format=fmt, level=logging.INFO)
  10. def ping(bot, update):
  11. """Check LCM ping response"""
  12. try:
  13. sp.check_output(['ping', '-c', '1', 'lcm.mi.infn.it'])
  14. except CalledProcessError, e:
  15. update.message.reply_text(text='LCM is unreachable')
  16. update.message.reply_text(text='LCM is reachable')
  17. def is_web_up(bot, update):
  18. """Check LCM http response"""
  19. try:
  20. conn = httplib.HTTPConnection('lcm.mi.infn.it:443')
  21. conn.request('HEAD', '/')
  22. s = conn.getresponse().status
  23. update.message.reply_text(text='Web server replied with code %s ' % s)
  24. except StandardError:
  25. update.message.reply_text(text='An error occurred while connecting')
  26. def vietnam(bot, update):
  27. """Spout wise words"""
  28. update.message.reply_text(text='Ricordate, ragazzi, LCM è come il Vietnam.'
  29. 'Una volta entrati, è impossibile uscirne!')
  30. def sell_your_mother(bot, update):
  31. """Remind people not to disclose passwords"""
  32. pic_id = 'AgADBAADbasxG9JPlAQNlEW3ML5sk_bEXxkABHKAFZ1ZzBZsNvMBAAEC'
  33. update.message.reply_photo(photo=pic_id, caption='cit. Mandelli')
  34. def print_msg_info(bot, update):
  35. """Print all message info to console - useful for debugging purposes"""
  36. print update.message
  37. def error(bot, update, error):
  38. """Log errors"""
  39. # create a logger with function scope ("static" object)
  40. error.logger = logging.getLogger(__name__)
  41. error.logger.warn('Update "%s" caused error "%s"' % (update, error))
  42. def abuse_150(bot, update):
  43. """Verbally abuse incompetent LCM collaborators"""
  44. insult = np.random.choice(insults)
  45. update.message.reply_text(text=insult)