From 5f122e70d3fd501913969275b911054466386db2 Mon Sep 17 00:00:00 2001 From: Jyri Eerola Date: Sat, 10 Jun 2017 20:02:53 +0300 Subject: [PATCH] Initial --- tbr.py | 130 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 130 insertions(+) create mode 100644 tbr.py diff --git a/tbr.py b/tbr.py new file mode 100644 index 0000000..5dc04fa --- /dev/null +++ b/tbr.py @@ -0,0 +1,130 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# +# msg.py +# +# Copyright 2017 Jyri Eerola +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, +# MA 02110-1301, USA. +# +# + +import json +import pprint +pp = pprint.PrettyPrinter() + +from telegram.ext import Updater, CommandHandler, MessageHandler, Filters +import logging + +# Enable logging +logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', + level=logging.INFO) + +logger = logging.getLogger(__name__) + + +# Define a few command handlers. These usually take the two arguments bot and +# update. Error handlers also receive the raised TelegramError object in error. +def start(bot, update): + update.message.reply_text('Hullo!') + + +def halp(bot, update): + update.message.reply_text('Halp!') + + +def fok(bot, update): + update.message.reply_text('Fok!') + + +def echo(bot, update): + update.message.reply_text(update.message.text) + + +def error(bot, update, error): + logger.warn('Update "%s" caused error "%s"' % (update, error)) + + +routingdict = dict() +def route(bot, update, args): + #global routingdict + chat_id = update.message.chat_id + source_id = ' '.join(args) + if source_id not in routingdict: + routingdict[source_id]=set() + routingdict[source_id].add(chat_id) + bot.send_message(chat_id=chat_id, text="New route: {} -> {}".format(source_id, chat_id)) + + +def print_routes(bot, update): + #global routingdict + update.message.reply_text(repr(routingdict)) + + +def main(token_file): + TG_TOKEN = open(token_file).read() + + # Create the EventHandler and pass it your bot's token. + updater = Updater(TG_TOKEN) + + # Get the dispatcher to register handlers + dp = updater.dispatcher + + # on different commands - answer in Telegram + dp.add_handler(CommandHandler("start", start)) + dp.add_handler(CommandHandler("help", halp)) + dp.add_handler(CommandHandler("fuck", fok)) + + # YEE + dp.add_handler(CommandHandler("print_routes", print_routes)) + dp.add_handler(CommandHandler("route", route, pass_args=True)) + + # on noncommand i.e message - echo the message on Telegram + dp.add_handler(MessageHandler(Filters.text, echo)) + + # log all errors + dp.add_error_handler(error) + + # Start the Bot + updater.start_polling() + + # PA-PA + #global routingdict + for line in sys.stdin: # CTRL+D, eli EOF, lopettaa + print('Line: {}'.format(line)) + parsed = json.loads(line) + source_id = parsed.get('source_id', '') + for chat_id in routingdict.get(source_id, {}): + if source_id == 'pandacraft': + if parsed['verb'] == 'joined': + updater.bot.send_message(chat_id=chat_id, text='Pandacraft: {} liittyi peliin'.format(parsed['player'])) + else: + updater.bot.send_message(chat_id=chat_id, text='Pandacraft: {} poistui'.format(parsed['player'])) + else: + updater.bot.send_message(chat_id=chat_id, text=line) + + # Run the bot until you press Ctrl-C or the process receives SIGINT, + # SIGTERM or SIGABRT. This should be used most of the time, since + # start_polling() is non-blocking and will stop the bot gracefully. + print('Entering idle...') + updater.idle() + + return 0 + +if __name__ == '__main__': + import sys + sys.exit(main(sys.argv[1])) +