almost ready

This commit is contained in:
Paolo Asperti 2017-12-26 19:29:54 +01:00
parent 2fa6f72f9e
commit 8fa4f415f5
5 changed files with 77 additions and 34 deletions

19
.editorconfig Normal file
View File

@ -0,0 +1,19 @@
# EditorConfig is awesome: http://EditorConfig.org
# top-most EditorConfig file
root = true
# Unix-style newlines with a newline ending every file
[*]
end_of_line = lf
insert_final_newline = true
# Matches multiple files with brace expansion notation
# Set default charset
[*.{js,py}]
charset = utf-8
# 4 space indentation
[*.py]
indent_style = space
indent_size = 4

1
TODO Normal file
View File

@ -0,0 +1 @@
- banlist (after XX attempts ban for XX hours, banned users list in config file)

View File

@ -2,7 +2,14 @@
daemon = false daemon = false
token = 347279594:AAH33Q3F2YYhAH3gkD6h2M3EqpH9H6Ht32c token = 347279594:AAH33Q3F2YYhAH3gkD6h2M3EqpH9H6Ht32c
pair_pin = 1234 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}] [id-173581]
achat_ids = [{u'username': u'pasperti', u'first_name': u'Paolo', u'last_name': u'Asperti', u'type': u'private', u'id': 173226581}] username = zuzu
firstname = pio
lastname = Pao
[id-173226581]
username = pasperti
firstname = Paolo
lastname = Asperti

View File

@ -2,4 +2,3 @@
daemon = false daemon = false
token = aaaaaaaaaaaaaaaaaaaaaaaaa token = aaaaaaaaaaaaaaaaaaaaaaaaa
pair_pin = 1234 pair_pin = 1234
chat_ids = ['121323', '343434', '123123123']

View File

@ -5,6 +5,7 @@ import datetime
import re import re
import ConfigParser import ConfigParser
import telepot import telepot
import json
from telepot.loop import MessageLoop from telepot.loop import MessageLoop
@ -15,25 +16,18 @@ def writeconfig():
def handle(msg): def handle(msg):
global configParser global configParser
# {u'date': 1495655440, u'text': u'asd', global chat_ids
# 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') 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'] chat_id = msg['chat']['id']
username = msg['chat']['username']
firstname = msg['chat']['first_name']
lastname = msg['chat']['last_name']
command = msg['text'] command = msg['text']
param = '' param = ''
# sta roba non funziona
paired_user = [x for x in chat_ids if x['id'] == chat_ids] paired_user = [x for x in chat_ids if x['id'] == chat_id]
is_paired = len(paired_user) > 0 is_paired = len(paired_user) > 0
if re.match('/',msg['text']): if re.match('/',msg['text']):
@ -41,18 +35,30 @@ def handle(msg):
command = re.sub(r"\s.*",'',msg['text']) command = re.sub(r"\s.*",'',msg['text'])
param = re.sub(r"^([^\s]+)\s","",msg['text']) param = re.sub(r"^([^\s]+)\s","",msg['text'])
print 'Got command: %s (%s)' % (command, param) print '\n\nUser %s sent command: %s (%s)' % (username, command, param)
if command == '/pair': if command == '/pair':
if is_paired: if is_paired:
bot.sendMessage(chat_id, 'You\'re already paired.') bot.sendMessage(chat_id, 'You\'re already paired.')
elif param == pair_pin: elif param == pair_pin:
chat_ids.append(msg['chat']) chat={
configParser.set('general', 'chat_ids', chat_ids) 'username': username,
'firstname': firstname,
'lastname': lastname,
'id': chat_id
}
chat_ids.append(chat)
section = 'id-' + str(chat_id)
if not configParser.has_section(section):
configParser.add_section(section)
configParser.set(section, 'username', username)
configParser.set(section, 'firstname', firstname)
configParser.set(section, 'lastname', lastname)
writeconfig() writeconfig()
bot.sendMessage(chat_id, 'Pairing ok.') bot.sendMessage(chat_id, 'Pairing ok.')
else: else:
bot.sendMessage(chat_id, 'Pairing failed: wrong pin.') bot.sendMessage(chat_id, 'Pairing failed: wrong pin.')
elif command == '/newpin': elif command == '/newpin':
if not is_paired: if not is_paired:
bot.sendMessage(chat_id, 'You\'re not allowed to do that. You have to pair first.') bot.sendMessage(chat_id, 'You\'re not allowed to do that. You have to pair first.')
@ -63,29 +69,40 @@ def handle(msg):
bot.sendMessage(chat_id, 'The new pin is: %s' % pair_pin) bot.sendMessage(chat_id, 'The new pin is: %s' % pair_pin)
else: else:
bot.sendMessage(chat_id, 'Please specify the new pin.') bot.sendMessage(chat_id, 'Please specify the new pin.')
elif command == '/unpair': elif command == '/unpair':
if not is_paired: if not is_paired:
bot.sendMessage(chat_id, 'You\'re not allowed to do that. You have to pair first.') bot.sendMessage(chat_id, 'You\'re not allowed to do that. You have to pair first.')
else: else:
# rimuovere dalla lista chat_ids[:] = [x for x in chat_ids if x['id'] != chat_id]
section = 'id-' + str(chat_id)
configParser.remove_section(section)
writeconfig()
bot.sendMessage(chat_id, 'Bye.') bot.sendMessage(chat_id, 'Bye.')
elif command == '/users': elif command == '/users':
users = [x['username'] + ' (' + x['first_name'] + ' ' + x['last_name'] + ')\n' for x in chat_ids] if not is_paired:
print users bot.sendMessage(chat_id, 'You\'re not allowed to do that. You have to pair first.')
else:
users = [x['username'] + ' (' + x['firstname'] + ' ' + x['lastname'] + ')\n' for x in chat_ids]
bot.sendMessage(chat_id, 'Paired users:\n' + "".join([str(i) for i in users]) ) bot.sendMessage(chat_id, 'Paired users:\n' + "".join([str(i) for i in users]) )
configParser = ConfigParser.RawConfigParser() configParser = ConfigParser.RawConfigParser()
configParser.read(r'telegram-notify.conf') configParser.read('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=[] chat_ids=[]
configParser.set('general', 'chat_ids', chat_ids) print "Allowed users: "
writeconfig() for p in configParser.sections():
if re.match('^id-.*',p):
chat_id={
'username': configParser.get(p,'username'),
'firstname': configParser.get(p,'firstname'),
'lastname': configParser.get(p,'lastname'),
'id': int(re.sub(r'^id-','',p))
}
chat_ids.append(chat_id)
print(" - (%s) %s %s" % (chat_id['username'],chat_id['firstname'],chat_id['lastname']) )
token = configParser.get('general', 'token') token = configParser.get('general', 'token')
bot = telepot.Bot(token) bot = telepot.Bot(token)