bot upgrade 1
This commit is contained in:
56
bot/bot.py
56
bot/bot.py
@@ -11,6 +11,8 @@ from telepot.delegate import (
|
||||
per_chat_id, pave_event_space, include_callback_query_chat_id, create_open, per_inline_from_id )
|
||||
from src.compiler import CrawlUrl
|
||||
from gevent import spawn, monkey, Greenlet
|
||||
import src.compiler.controller as compiler_controller
|
||||
import src.articles.controller as articles_controller
|
||||
|
||||
def IKB(h):
|
||||
return InlineKeyboardButton(text=h["text"], callback_data=h["callback_data"])
|
||||
@@ -23,35 +25,72 @@ def IKM(h):
|
||||
def IKM2(h):
|
||||
return InlineKeyboardMarkup(inline_keyboard= map(IKB2,h))
|
||||
|
||||
def is_admin(id):
|
||||
lg.debug("check admin?"+str(id))
|
||||
if str(id) in cfg.is_admin:
|
||||
lg.debug("is admin")
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
|
||||
def query_que_url(url):
|
||||
print(json.dumps(url))
|
||||
return {"text": url.url, "callback_data":"/urls/"+str(url.id)+"/que"}
|
||||
def query_test_url(url):
|
||||
print(json.dumps(url))
|
||||
return {"text": url.url, "callback_data":"/urls/"+str(url.id)+"/test"}
|
||||
|
||||
def handle_urls(handler, cmd):
|
||||
curls=CrawlUrl.query.all()
|
||||
#sent=handler.sender.sendMessage(json.dumps(curls))
|
||||
kb= IKM2(map(query_que_url,curls))
|
||||
kb_que= IKM2(map(query_que_url,curls))
|
||||
kb_test= IKM2(map(query_test_url,curls))
|
||||
kb_url=IKM2([{"text": "Que an url", "callback_data":"/urls/que"},
|
||||
{"text": "Test an url", "callback_data":"/urls/test"}
|
||||
])
|
||||
lg.debug(json.dumps(cmd))
|
||||
print json.dumps(cmd)
|
||||
if len(cmd) >= 4 and cmd[3]=="que":
|
||||
sent=handler.sender.sendMessage("I qued url "+str(cmd[2]), reply_markup=None)
|
||||
sent=handler.sender.sendMessage("I qued url "+ json.dumps(compiler_controller.urls_test(int(cmd[2]))), reply_markup=None)
|
||||
elif len(cmd) >= 4 and cmd[3]=="test":
|
||||
sent=handler.sender.sendMessage("I tested url: "+ json.dumps(compiler_controller.urls_test(int(cmd[2]))["comp"])[0:399], reply_markup=None)
|
||||
elif len(cmd) >= 3 and cmd[2] == "que":
|
||||
sent=handler.sender.sendMessage("Which url shoud I que?", reply_markup=kb_que)
|
||||
handler._edit_msg_ident = telepot.message_identifier(sent)
|
||||
handler._editor = telepot.helper.Editor(handler.bot, sent)
|
||||
elif len(cmd) >= 3 and cmd[2] == "test":
|
||||
sent=handler.sender.sendMessage("Which url shoud I test?", reply_markup=kb_test)
|
||||
handler._edit_msg_ident = telepot.message_identifier(sent)
|
||||
handler._editor = telepot.helper.Editor(handler.bot, sent)
|
||||
else:
|
||||
sent=handler.sender.sendMessage("que?", reply_markup=kb)
|
||||
sent=handler.sender.sendMessage("What do you want to do?", reply_markup=kb_url)
|
||||
handler._edit_msg_ident = telepot.message_identifier(sent)
|
||||
handler._editor = telepot.helper.Editor(handler.bot, sent)
|
||||
|
||||
def execute_command(handler,cmd,msg=None):
|
||||
def execute_command(handler,cmd,msg=None, args=[]):
|
||||
if cmd[1]=='urls':
|
||||
handle_urls(handler,cmd)
|
||||
|
||||
|
||||
if is_admin(msg["from"]["id"]):
|
||||
handle_urls(handler,cmd)
|
||||
else:
|
||||
handler.sender.sendMessage("Not allowed for "+json.dumps(msg["from"]))
|
||||
elif cmd[1] =='articles':
|
||||
handler.sender.sendMessage(json.dumps({"args": args, "cmd": cmd}))
|
||||
handler.sender.sendMessage(json.dumps(articles_controller.search(args[0])))
|
||||
elif cmd[1] =='startworkers':
|
||||
if is_admin(msg["from"]["id"]):
|
||||
handler.sender.sendMessage(compiler_controller.start_workers())
|
||||
|
||||
else:
|
||||
handler.sender.sendMessage("Sorry, I didn't understand the command!")
|
||||
|
||||
def handle(handler,msg):
|
||||
content_type,chat_type,chat_id = telepot.glance(msg)
|
||||
if msg.has_key('text'):
|
||||
if msg['text'][0]=='/':
|
||||
cmd = msg['text'].split("/")
|
||||
execute_command(handler, cmd, msg)
|
||||
args=cmd[-1].split(" ")[1:]
|
||||
cmd[-1]=cmd[-1].split(" ")[0]
|
||||
execute_command(handler, cmd, msg,args)
|
||||
if msg.has_key('data'):
|
||||
lg.debug(msg['data'])
|
||||
|
||||
@@ -99,6 +138,7 @@ class FetBot(telepot.helper.ChatHandler):
|
||||
handle(self,msg)
|
||||
content_type,chat_type,chat_id = telepot.glance(msg)
|
||||
lg.debug(content_type)
|
||||
lg.debug(msg)
|
||||
if content_type=="photo" or content_type=="sticker":
|
||||
lg.debug("try to download %s" % msg[content_type][-1]["file_id"])
|
||||
f=self.bot.getFile(msg[content_type][-1]['file_id'])
|
||||
|
||||
Reference in New Issue
Block a user