handlers.py 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. # -*- coding: utf-8 -*-
  2. import subprocess as sp
  3. import httplib
  4. import numpy as np
  5. from insults import insults
  6. def ping(bot, update):
  7. """ Check LCM ping response """
  8. try:
  9. sp.check_output(['ping', '-c', '1', 'lcm.mi.infn.it'])
  10. except CalledProcessError, e:
  11. bot.sendMessage(chat_id=update.message.chat_id,
  12. text='LCM is unreachable')
  13. bot.sendMessage(chat_id=update.message.chat_id,
  14. text='LCM is reachable')
  15. def is_web_up(bot, update):
  16. """ Check LCM http response """
  17. try:
  18. conn = httplib.HTTPConnection('lcm.mi.infn.it:443')
  19. conn.request('HEAD', '/')
  20. s = conn.getresponse().status
  21. bot.sendMessage(chat_id=update.message.chat_id,
  22. text='Web server replied with code %s ' % s)
  23. except StandardError:
  24. bot.sendMessage(chat_id=update.message.chat_id,
  25. text='An error occurred while connecting')
  26. def vietnam(bot, update):
  27. """ Spout wise words """
  28. bot.sendMessage(chat_id=update.message.chat_id,
  29. text='Ricordate, ragazzi, LCM è come il Vietnam. Una volta \
  30. entrati, è impossibile uscirne!')
  31. def sell_your_mother(bot, update):
  32. """ Remind people not to disclose passwords """
  33. pic_id = 'AgADBAADbasxG9JPlAQNlEW3ML5sk_bEXxkABHKAFZ1ZzBZsNvMBAAEC'
  34. bot.sendPhoto(chat_id=update.message.chat_id, photo=pic_id,
  35. caption='cit. Mandelli')
  36. def tell_a_tale(bot, update):
  37. """ Tell a story about LCM """
  38. stories = [ 'Non conosco ancora nessuna storia. \
  39. Clona il mio repo e insegnamene qualcuna!' ]
  40. story = np.random.choice(stories)
  41. bot.sendMessage(chat_id=update.message.chat_id, text=story)
  42. def print_msg_info(bot, update):
  43. """ Print all message info to console - useful for debugging purposes """
  44. print update.message
  45. def error(bot, update, error):
  46. """ Log errors """
  47. logger.warn('Update "%s" caused error "%s"' % (update, error))
  48. def abuse_150(bot, update):
  49. """ Verbally abuse incompetent LCM collaborators """
  50. insult = np.random.choice(insults)
  51. bot.sendMessage(chat_id=update.message.chat_id, text=insult)