From 2fa6f72f9e1e320b3c386987e22b0e21bcf34bc9 Mon Sep 17 00:00:00 2001 From: paspo Date: Tue, 26 Dec 2017 11:59:09 +0100 Subject: [PATCH] initial commit --- telegram-notify.conf | 8 +++ telegram-notify.conf.default | 5 ++ telegram-notify.py | 97 ++++++++++++++++++++++++++++++++++++ 3 files changed, 110 insertions(+) create mode 100644 telegram-notify.conf create mode 100644 telegram-notify.conf.default create mode 100644 telegram-notify.py diff --git a/telegram-notify.conf b/telegram-notify.conf new file mode 100644 index 0000000..c0d854c --- /dev/null +++ b/telegram-notify.conf @@ -0,0 +1,8 @@ +[general] +daemon = false +token = 347279594:AAH33Q3F2YYhAH3gkD6h2M3EqpH9H6Ht32c +pair_pin = 1234 +elenco = ['asd', 'sdsdsd', 'sdfsffsd'] +chat_ids = [{u'username': u'pasperti', u'first_name': u'Paolo', u'last_name': u'Asperti', u'type': u'private', u'id': 173226581}, {u'username': u'pasperti', u'first_name': u'Paolo', u'last_name': u'Asperti', u'type': u'private', u'id': 173226581}, {u'username': u'pasperti', u'first_name': u'Paolo', u'last_name': u'Asperti', u'type': u'private', u'id': 173226581}] +achat_ids = [{u'username': u'pasperti', u'first_name': u'Paolo', u'last_name': u'Asperti', u'type': u'private', u'id': 173226581}] + diff --git a/telegram-notify.conf.default b/telegram-notify.conf.default new file mode 100644 index 0000000..0d9e171 --- /dev/null +++ b/telegram-notify.conf.default @@ -0,0 +1,5 @@ +[general] +daemon = false +token = aaaaaaaaaaaaaaaaaaaaaaaaa +pair_pin = 1234 +chat_ids = ['121323', '343434', '123123123'] diff --git a/telegram-notify.py b/telegram-notify.py new file mode 100644 index 0000000..eb7a742 --- /dev/null +++ b/telegram-notify.py @@ -0,0 +1,97 @@ +#!/usr/bin/python +import time +import random +import datetime +import re +import ConfigParser +import telepot +from telepot.loop import MessageLoop + + +def writeconfig(): + global configParser + with open('telegram-notify.conf', 'wb') as configfile: + configParser.write(configfile) + +def handle(msg): + global configParser + # {u'date': 1495655440, u'text': u'asd', + # u'from': {u'username': u'pasperti', u'first_name': u'Paolo', u'last_name': u'Asperti', u'id': 173226581, u'language_code': u'it'}, + # u'message_id': 64, + # u'chat': {u'username': u'pasperti', u'first_name': u'Paolo', u'last_name': u'Asperti', u'type': u'private', u'id': 173226581}} + + pair_pin = configParser.get('general', 'pair_pin') + chat_ids = configParser.get('general', 'chat_ids') + + print "pair pin" + print pair_pin + + print "chat ids" + print chat_ids + + chat_id = msg['chat']['id'] + command = msg['text'] + param = '' + # sta roba non funziona + paired_user = [x for x in chat_ids if x['id'] == chat_ids] + is_paired = len(paired_user) > 0 + + if re.match('/',msg['text']): + # e' un comando + command = re.sub(r"\s.*",'',msg['text']) + param = re.sub(r"^([^\s]+)\s","",msg['text']) + + print 'Got command: %s (%s)' % (command, param) + + if command == '/pair': + if is_paired: + bot.sendMessage(chat_id, 'You\'re already paired.') + elif param == pair_pin: + chat_ids.append(msg['chat']) + configParser.set('general', 'chat_ids', chat_ids) + writeconfig() + bot.sendMessage(chat_id, 'Pairing ok.') + else: + bot.sendMessage(chat_id, 'Pairing failed: wrong pin.') + elif command == '/newpin': + if not is_paired: + bot.sendMessage(chat_id, 'You\'re not allowed to do that. You have to pair first.') + elif param != '': + pair_pin = param + configParser.set('general', 'pair_pin', pair_pin) + writeconfig() + bot.sendMessage(chat_id, 'The new pin is: %s' % pair_pin) + else: + bot.sendMessage(chat_id, 'Please specify the new pin.') + elif command == '/unpair': + if not is_paired: + bot.sendMessage(chat_id, 'You\'re not allowed to do that. You have to pair first.') + else: + # rimuovere dalla lista + bot.sendMessage(chat_id, 'Bye.') + elif command == '/users': + users = [x['username'] + ' (' + x['first_name'] + ' ' + x['last_name'] + ')\n' for x in chat_ids] + print users + bot.sendMessage(chat_id, 'Paired users:\n' + "".join([str(i) for i in users]) ) + + +configParser = ConfigParser.RawConfigParser() +configParser.read(r'telegram-notify.conf') + +chat_ids = configParser.get('general', 'chat_ids') +print "type: " +print type(chat_ids) +print chat_ids +if type(chat_ids) is str: + chat_ids=[] + configParser.set('general', 'chat_ids', chat_ids) + writeconfig() + +token = configParser.get('general', 'token') +bot = telepot.Bot(token) + +MessageLoop(bot, handle).run_as_thread() +print 'I am listening ...' + +while 1: + time.sleep(10)