Browse Source

Initial commit

bluehood 4 years ago
commit
36bca8f2d0
6 changed files with 130 additions and 0 deletions
  1. 3 0
      .gitignore
  2. 36 0
      LCMbot.py
  3. 3 0
      LICENSE
  4. 8 0
      README.md
  5. 62 0
      handlers.py
  6. 18 0
      wishlist.md

+ 3 - 0
.gitignore

@@ -0,0 +1,3 @@
+*.swp
+*.pyc
+*~

+ 36 - 0
LCMbot.py

@@ -0,0 +1,36 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+from telegram.ext import Updater, CommandHandler, MessageHandler
+import logging
+import handlers as hnd
+
+# enable logging
+logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
+                            level=logging.INFO)
+logger = logging.getLogger(__name__)
+
+
+def main():
+    # create the EventHandler
+    updater = Updater("TOKEN")
+
+    # register handlers
+    dp = updater.dispatcher
+    dp.add_handler(CommandHandler("pinglcm", hnd.ping))
+    dp.add_handler(CommandHandler("checkwebsite", hnd.is_web_up))
+    dp.add_handler(CommandHandler("vietnam", hnd.vietnam))
+    dp.add_handler(CommandHandler("sellyourmother", hnd.sell_your_mother))
+    dp.add_handler(CommandHandler("storytime", hnd.tell_a_tale))
+    dp.add_error_handler(hnd.error)
+    # dp.add_handler(MessageHandler([], hnd.print_msg_info))
+
+    # start LCMbot
+    updater.start_polling()
+
+    # run until the process receives SIGINT, SIGTERM or SIGABRT
+    updater.idle()
+
+
+if __name__ == '__main__':
+    main()

+ 3 - 0
LICENSE

@@ -0,0 +1,3 @@
+This software is licensed under the Academic Free License version 3.0.
+
+A copy of the license can be found at https://opensource.org/licenses/AFL-3.0

+ 8 - 0
README.md

@@ -0,0 +1,8 @@
+# Utility bot for common LCM tasks
+
+Powered by [Python Telegram Bot](https://github.com/python-telegram-bot/python-telegram-bot) (documentation [here](https://pythonhosted.org/python-telegram-bot/)).
+Requires python 2.7, python-telegram-bot and numpy.
+
+The project complies with [PEP 8](https://www.python.org/dev/peps/pep-0008/) coding guidelines as much as humanly possible.
+
+Licensed under the Academic Free License version 3.0.

+ 62 - 0
handlers.py

@@ -0,0 +1,62 @@
+# -*- coding: utf-8 -*-
+import subprocess as sp
+import httplib
+import numpy as np
+
+
+def ping(bot, update):
+    """ Check LCM ping response """
+    try:
+        sp.check_output(['ping', '-c', '1', 'lcm.mi.infn.it'])
+    except CalledProcessError, e:
+        bot.sendMessage(chat_id=update.message.chat_id,
+                        text='LCM is unreachable')
+    bot.sendMessage(chat_id=update.message.chat_id,
+                    text='LCM is reachable')
+
+
+def is_web_up(bot, update):
+    """ Check LCM http response """
+    try:
+        conn = httplib.HTTPConnection('lcm.mi.infn.it:443')
+        conn.request('HEAD', '/')
+        s = conn.getresponse().status
+        bot.sendMessage(chat_id=update.message.chat_id,
+                        text='Web server replied with code %s ' % s)
+    except StandardError:
+        bot.sendMessage(chat_id=update.message.chat_id,
+                        text='An error occurred while connecting')
+
+
+def vietnam(bot, update):
+    """ Spout wise words """
+    bot.sendMessage(chat_id=update.message.chat_id,
+                    text='Ricordate, ragazzi, LCM è come il Vietnam. Una volta \
+entrati, è impossibile uscirne!')
+
+
+def sell_your_mother(bot, update):
+    """ Remind people not to disclose passwords """
+    pic_id = 'AgADBAADbasxG9JPlAQNlEW3ML5sk_bEXxkABHKAFZ1ZzBZsNvMBAAEC'
+    bot.sendPhoto(chat_id=update.message.chat_id, photo=pic_id,
+                  caption='cit. Mandelli')
+
+
+def tell_a_tale(bot, update):
+    """ Tell a story about LCM """
+    stories = [ 'Non conosco ancora nessuna storia. \
+Clona il mio repo e insegnamene qualcuna!' ]
+    story = np.random.choice(stories)
+    bot.sendMessage(chat_id=update.message.chat_id,
+                    text=story)
+
+
+def print_msg_info(bot, update):
+    """ Print all message info to console - useful for debugging purposes """
+    print update.message
+
+
+def error(bot, update, error):
+    """ Log errors """
+    logger.warn('Update "%s" caused error "%s"' % (update, error))
+

+ 18 - 0
wishlist.md

@@ -0,0 +1,18 @@
+## COMMANDS
+* abuse 150
+* whoall
+* list down nodes
+
+## INFRASTRUCTURE
+Ideally, LCMbot should have to do as little as possible to deliver its services.
+LCMbot should only serve as an easy point of access to information.
+LCM should provide this information to LCMbot in a machine-friendly format,
+possibly through an API only reachable from within LCM network.
+
+For example, this is my idea of how this would go down for `whoall`:
+LCMbot is running on a VM, inside LCM network. It communicates with telegram 
+bot API via http, and http is its only link to the outside world. When LCMbot
+receives the `/whoall` command, it interrogates a running service inside LCM
+network and retrieves machine readable information on who is connected where.
+LCMbot's only concern is reshaping the data in a human readable form and send
+it as a telegram message.