handlers.py 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. bot.send_message(chat_id=update.message.chat_id,
  16. text='LCM is unreachable')
  17. bot.send_message(chat_id=update.message.chat_id,
  18. text='LCM is reachable')
  19. def is_web_up(bot, update):
  20. """Check LCM http response"""
  21. try:
  22. conn = httplib.HTTPConnection('lcm.mi.infn.it:443')
  23. conn.request('HEAD', '/')
  24. s = conn.getresponse().status
  25. bot.send_message(chat_id=update.message.chat_id,
  26. text='Web server replied with code %s ' % s)
  27. except StandardError:
  28. bot.send_message(chat_id=update.message.chat_id,
  29. text='An error occurred while connecting')
  30. def vietnam(bot, update):
  31. """Spout wise words"""
  32. bot.send_message(chat_id=update.message.chat_id,
  33. text='Ricordate, ragazzi, LCM è come il Vietnam. Una volta \
  34. entrati, è impossibile uscirne!')
  35. def sell_your_mother(bot, update):
  36. """Remind people not to disclose passwords"""
  37. pic_id = 'AgADBAADbasxG9JPlAQNlEW3ML5sk_bEXxkABHKAFZ1ZzBZsNvMBAAEC'
  38. bot.sendPhoto(chat_id=update.message.chat_id, photo=pic_id,
  39. caption='cit. Mandelli')
  40. def tell_a_tale(bot, update):
  41. """ Tell a story about LCM """
  42. stories = [ 'Non conosco ancora nessuna storia. \
  43. Clona il mio repo e insegnamene qualcuna!' ]
  44. story = np.random.choice(stories)
  45. bot.sendMessage(chat_id=update.message.chat_id, text=story)
  46. def print_msg_info(bot, update):
  47. """Print all message info to console - useful for debugging purposes"""
  48. print update.message
  49. def error(bot, update, error):
  50. """Log errors"""
  51. # create a logger with function scope ("static" object)
  52. error.logger = logging.getLogger(__name__)
  53. error.logger.warn('Update "%s" caused error "%s"' % (update, error))
  54. def abuse_150(bot, update):
  55. """Verbally abuse incompetent LCM collaborators"""
  56. insult = np.random.choice(insults)
  57. bot.send_message(chat_id=update.message.chat_id, text=insult)