introduce search interface

This commit is contained in:
www
2020-12-07 09:57:30 +00:00
parent 894a99cdef
commit ad36db964e
21 changed files with 32004 additions and 52 deletions

View File

@@ -59,14 +59,8 @@ def download_file(url):
return local_filename
class Chat(SaveFileObject):
class BaseChat(SaveFileObject):
"Basis für einen Telegram Chat"
def __init__(self,id, bot):
self.id=id
#self.chat_id=chat_id
@@ -84,8 +78,6 @@ class Chat(SaveFileObject):
"type": self.type,
"mode": self.mode
}
def asdict(self):
return self.to_dict()
@classmethod
def from_dict(self, bot, d):
c=Chat(d["id"],bot)
@@ -94,20 +86,17 @@ class Chat(SaveFileObject):
c.mode=d.get("mode","")
return c
def inline(self, query):
InlineQueryResultArticle
r=solr.search("text_txt: %s" % query.query,sort="date_dt desc").docs
r= [fet.find_one({"slug": rr["id"]}) for rr in r ]
res=[ InlineQueryResultArticle(
id=d["url"],
title=d["title"],
url=urljoin(TARGET,d["url"]),
thumb_url = urljoin(TARGET,d["image"]),
input_message_content = InputTextMessageContent(message_text=urljoin(TARGET,d["url"]))
) for d in r]
links=[p["title"]+": "+"(%s) "% p["public_date"] + urljoin(TARGET,p["url"]) for p in r]
self.bot.answer_inline_query(inline_query_id=query.id,results=res )
return
def echo(self,update):
self.reply_msg(yaml.dump(update._raw)+"\nfrom"+str(get_from_id(update)))
def send_msg(self, text,**kwargs):
self.bot.send_message(chat_id=self.id, text=text, **kwargs)
def reply_msg(self, text, **kwargs):
self.bot.send_message(chat_id=self.id, text=text, reply_to_message_id=self.last_message_id,**kwargs)
def asdict(self):
return self.to_dict()
def inline(self,query):
pass
def process_update(self,update):
@@ -137,7 +126,7 @@ class Chat(SaveFileObject):
text=update.callback_query.message.text+": "+update.callback_query.data)
if update.message and update.message.photo:
ff=max(update.message.photo, key=lambda x: x.file_size)
ff=max(update.message.photo, key=lambda x: x.file_size) # Foto mit bester Auflösung
fff = self.bot.get_file(ff.file_id)
url=fff.get_download_url(API_KEY)
r = requests.get(url)
@@ -150,19 +139,28 @@ class Chat(SaveFileObject):
u=users[update]
users.to_file()
def echo(self,update):
self.reply_msg(yaml.dump(update._raw)+"\nfrom"+str(get_from_id(update)))
def send_msg(self, text,**kwargs):
self.bot.send_message(chat_id=self.id, text=text, **kwargs)
def reply_msg(self, text, **kwargs):
self.bot.send_message(chat_id=self.id, text=text, reply_to_message_id=self.last_message_id,**kwargs)
class Chat(BaseChat):
def inline(self, query):
#r=solr.search("text_txt: %s" % ,sort="date_dt desc").docs
links,hits=solr.search(query.query)
r= links[0:6]
res=[ InlineQueryResultArticle(
id=d["url"],
title=d["title"],
url=urljoin(TARGET,d["url"]),
thumb_url = urljoin(TARGET,d["image"]),
input_message_content = InputTextMessageContent(message_text=urljoin(TARGET,d["url"]))
) for d in r]
#links=[p["title"]+": "+"(%s) "% p["public_date"] + urljoin(TARGET,p["url"]) for p in r]
self.bot.answer_inline_query(inline_query_id=query.id,results=res )
return
def process_command(self,cmd,txt,update):
self.last_message_id=get_message_id(update)
u=users[update]
print("--\ndict: %s" % str(users.asdict()))
# print("--\ndict: %s" % str(users.asdict()))
#users[get_from_id(update)]=u
if cmd == "/s" or cmd =="/search":
@@ -179,12 +177,16 @@ class Chat(SaveFileObject):
self.reply_msg("Mode: %s" % txt)
return True
elif cmd == "/debug":
if not u.fet_user:
self.reply_msg("bitte vorher /auth ausführen wenn du ein FET Mitglied bist")
return True
if self.debug:
self.debug = False
else:
self.debug = True #not self.debug
self.reply_msg("Debug: %s" % str(self.debug))
return True
elif cmd =="/post":
if not u.fet_user:
self.reply_msg("bitte vorher /auth ausführen wenn du ein FET Mitglied bist")
@@ -192,6 +194,9 @@ class Chat(SaveFileObject):
self.workflows[get_from_id(update)]=CreatePostWorkflow(chat=self)
return True
elif cmd == "/reindex":
if not u.fet_user:
self.reply_msg("bitte vorher /auth ausführen wenn du ein FET Mitglied bist")
return True
self.reply_msg("Das kann ein bissl dauern...")
solr.reindex()
self.send_msg("Fertig mit dem neuen Index")
@@ -221,6 +226,7 @@ def get_command_from_update(update):
class ChatManager(SaveFileMapping):
def __init__(self,bot):

View File

@@ -29,17 +29,16 @@ class TelegramWorkflow(TelegramStateMachine):
class CreatePostWorkflow(TelegramWorkflow):
# init --> wait step 0 --> confirm --> wait step 1--> confirm -->wait_photo --> finished
init =State('init', initial=True) # Initialize the workflow
finished = State('finished') # Workflow has ended
wait = State('wait',value=0)
confirm =State('confirm')
# finished = State('finished')
wait_photo=State('wait_photo')
initialize = init.to(wait) # init --> wait
entered = wait.to(confirm) # wait --> confirm
next=confirm.to(wait) # confirm --> wait
@@ -98,7 +97,7 @@ class CreatePostWorkflow(TelegramWorkflow):
#elif self.current_state == self.wait_photo:
if media:
if self.debug:
self.send("Workflow received media -- yey")
self.send("Danke fürs Foto")
self.p["files"]={"image":media}
if self.current_state == self.wait_photo:
self.finish()
@@ -118,8 +117,6 @@ class CreatePostWorkflow(TelegramWorkflow):
if self.debug: self.send("retry")
def on_cancel(self):
#self.p={}
#self.step=0
if self.debug: self.send("Canceled")
def on_finish(self):
@@ -130,12 +127,12 @@ class CreatePostWorkflow(TelegramWorkflow):
self.p["public_date"]=datetime.date.today()
self.send("posting to %s" % TARGET)
r,d=fet.create(self.p)
print(r)
logger.info(r)
if r ==201:
self.send("Eingabe fertig:%s" % str(urljoin(TARGET,"posts/"+d["slug"])))
else:
self.send("Fehler beim posten -- sorry")
def on_enter_confirm(self):
self.send_confirm("Bitte die Eingabe von %s bestaetigen" % self.value)