Created
April 1, 2016 09:48
-
-
Save anonymous/b690bfe36bf4f454b49376fa3ac80ff4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
import asyncio | |
import telepot | |
import telepot.async | |
import sqlite3 | |
import json | |
import requests | |
import time | |
import sys | |
import re | |
from db import Connect | |
from threading import Thread | |
@asyncio.coroutine | |
def on_chat_message(msg): | |
content_type, chat_type, chat_id = telepot.glance(msg); | |
message = msg['text']; | |
fromUser = msg['from']['username']; | |
name = msg['from']['first_name'] + ' ' + msg['from']['last_name']; | |
print('<- [' + str(chat_id) + '|' + content_type + '|' + chat_type + ']' + fromUser + ': ' + message); | |
yield from command(nome = name, fromUsername = fromUser, contentType = content_type, chatType = chat_type, chatId = chat_id, key = message); | |
@asyncio.coroutine | |
def command(nome, fromUsername, contentType, chatType, chatId, key): | |
if(contentType == 'text' and chatType == 'private'): | |
if (not ' ' in key and key == 'cadastrar'): | |
if(botDB.addUser(nome, fromUsername, chatId, 1)): | |
yield from sM(chatId, 'Você foi cadastrado com sucesso!'); | |
else: | |
yield from sM(chatId, 'Você já está cadastrado em nosso banco de dados.'); | |
elif(' ' in key and 'cadastrar usuario' in key): | |
if(botDB.checkUserAdmin(fromUsername, chatId)): | |
userdetails = key.split(' '); | |
if(botDB.addUser(userdetails[2] + ' ' + userdetails[3], userdetails[4], userdetails[5])): | |
yield from sM(chatId, 'Usuário cadastrado com sucesso!'); | |
else: | |
yield from sM(chatId, 'Usuário já cadastrado em nosso banco de dados.'); | |
else: | |
yield from sM(chatId, 'Você não tem privilégios de administrador para realizar esta ação.'); | |
if (not ' ' in key and key == 'remover'): | |
if(botDB.checkUserAdmin(fromUsername, chatId)): | |
if(botDB.removeUser(fromUsername, chatId)): | |
yield from sM(chatId, 'Usuário removido com sucesso!'); | |
else: | |
yield from sM(chatId, 'Erro ao remover usuário.'); | |
else: | |
yield from sM(chatId, 'Você não tem privilégios de administrador para realizar esta ação.'); | |
elif(' ' in key and 'remover usuario' in key): | |
if(botDB.checkUserAdmin(fromUsername, chatId)): | |
userdetails = key.split(' '); | |
if(botDB.removeUser(userdetails[2], userdetails[3])): | |
yield from sM(chatId, 'Usuário removido com sucesso!'); | |
else: | |
yield from sM(chatId, 'Erro ao remover usuário.'); | |
else: | |
yield from sM(chatId, 'Você não tem privilégios de administrador para realizar esta ação.'); | |
if('find user' in key or 'buscar usuário' in key or 'procurar usuário' in key): | |
username = key.split(' '); | |
if(botDB.checkUserAdmin(fromUsername, chatId)): | |
details = botDB.findUserByNick(username[2]); | |
if(details): | |
yield from sM(chatId, 'Usuario encontrado\n\nId: {}\nNome: {}\nUsername: {}\nChatId: {}\nAtivo: {}\nUserLevel: {}'.format(details[0][0], details[0][1], details[0][2], details[0][3], details[0][4], details[0][5])); | |
else: | |
yield from sM(chatId, 'Nenhum registro encontrado para os critérios pesquisados.'); | |
else: | |
yield from sM(chatId, 'Você não tem privilégios de administrador para realizar esta ação.'); | |
if(key == 'get my id' or key == 'qual meu id' or key == 'meu id'): | |
yield from sM(chatId, 'Seu ID é {}'.format(chatId)); | |
def getJSON(): | |
db = Connect('bot.db'); | |
ultimoCadastrado = ''; | |
while 1: | |
try: | |
getJson = requests.get('https://api.import.io/store/connector/0f73e47a-bc1d-490b-924b-8886ac703701/_query?input=webpage/url:https%3A%2F%2Fwww.gatry.com%2F&&_apikey=000c112af5ad4f33b2f40fdfe34ec36ba64eea2cfb0998639867a151bdaf65f498dc0376a779863af592d87e58c3291732681793b89008f48fbc545a9cb4c34eb6ebc3b5a5695508a964ed5cdda7eb9d'); | |
data = json.loads(getJson.text); | |
if('results' in data): | |
nomeP = data['results'][0]['nome']; | |
valorP = data['results'][0]['valor']; | |
linkP = data['results'][0]['link_gatry']; | |
findData = re.search('title=\"([^\"]+)', str(data), re.I); | |
dataP = findData.group(1); | |
if(nomeP != ultimoCadastrado): | |
print('Promoção encontrada! -> ', nomeP); | |
print('Verificando em nossos bancos de dados...'); | |
'''--> i need to call it here sM('154296610','hello');''' | |
if not(db.checkPromoDuplicate(nomeP, valorP, dataP, linkP)): | |
db.addPromo(nomeP, valorP, dataP, linkP); | |
ultimoCadastrado = nomeP; | |
else: | |
print('KeyError: a chave não foi encontrada no JSON.'); | |
except: | |
pass | |
time.sleep(10); | |
def sM(chatId, message): | |
yield from bot.sendMessage(chatId, '<code>{}</code>'.format(message), parse_mode='HTML'); | |
TOKEN = sys.argv[1]; # get token from command-line | |
bot = telepot.async.Bot(TOKEN) | |
threadJson = Thread(target = getJSON); | |
threadJson.setDaemon(True); | |
threadJson.start(); | |
loop = asyncio.get_event_loop() | |
botDB = Connect('bot.db'); | |
loop.create_task(bot.messageLoop(on_chat_message)) | |
print('Listening ...') | |
loop.run_forever() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment