handlers.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. # -*- coding: utf-8 -*-
  2. import subprocess as sp
  3. import httplib
  4. import numpy as np
  5. def ping(bot, update):
  6. """ Check LCM ping response """
  7. try:
  8. sp.check_output(['ping', '-c', '1', 'lcm.mi.infn.it'])
  9. except CalledProcessError, e:
  10. bot.sendMessage(chat_id=update.message.chat_id,
  11. text='LCM is unreachable')
  12. bot.sendMessage(chat_id=update.message.chat_id,
  13. text='LCM is reachable')
  14. def is_web_up(bot, update):
  15. """ Check LCM http response """
  16. try:
  17. conn = httplib.HTTPConnection('lcm.mi.infn.it:443')
  18. conn.request('HEAD', '/')
  19. s = conn.getresponse().status
  20. bot.sendMessage(chat_id=update.message.chat_id,
  21. text='Web server replied with code %s ' % s)
  22. except StandardError:
  23. bot.sendMessage(chat_id=update.message.chat_id,
  24. text='An error occurred while connecting')
  25. def vietnam(bot, update):
  26. """ Spout wise words """
  27. bot.sendMessage(chat_id=update.message.chat_id,
  28. text='Ricordate, ragazzi, LCM è come il Vietnam. Una volta \
  29. entrati, è impossibile uscirne!')
  30. def sell_your_mother(bot, update):
  31. """ Remind people not to disclose passwords """
  32. pic_id = 'AgADBAADbasxG9JPlAQNlEW3ML5sk_bEXxkABHKAFZ1ZzBZsNvMBAAEC'
  33. bot.sendPhoto(chat_id=update.message.chat_id, photo=pic_id,
  34. caption='cit. Mandelli')
  35. def tell_a_tale(bot, update):
  36. """ Tell a story about LCM """
  37. stories = [ 'Non conosco ancora nessuna storia. \
  38. Clona il mio repo e insegnamene qualcuna!' ]
  39. story = np.random.choice(stories)
  40. bot.sendMessage(chat_id=update.message.chat_id,
  41. 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))