mirror of
https://gitlab.com/simple-stock-bots/simple-telegram-stock-bot.git
synced 2025-07-25 07:31:48 +00:00
fixed inline #82
This commit is contained in:
46
bot.py
46
bot.py
@@ -261,8 +261,8 @@ def information(update: Update, context: CallbackContext):
|
||||
|
||||
def search(update: Update, context: CallbackContext):
|
||||
"""
|
||||
Uses fuzzy search on full list of stocks and crypto names
|
||||
and descriptions then returns the top matches in order.
|
||||
Searches on full list of stocks and crypto descriptions
|
||||
then returns the top matches in order of smallest symbol name length.
|
||||
"""
|
||||
info(f"Search command ran by {update.message.chat.username}")
|
||||
message = update.message.text.replace("/search ", "")
|
||||
@@ -275,11 +275,13 @@ def search(update: Update, context: CallbackContext):
|
||||
return
|
||||
|
||||
context.bot.send_chat_action(chat_id=chat_id, action=telegram.ChatAction.TYPING)
|
||||
queries = s.search_symbols(message)[:10]
|
||||
if queries:
|
||||
reply = "*Search Results:*\n`$ticker: Company Name`\n`" + ("-" * 21) + "`\n"
|
||||
for query in queries:
|
||||
reply += "`" + query[1] + "`\n"
|
||||
queries = s.inline_search(message, matches=10)
|
||||
if not queries.empty:
|
||||
reply = "*Search Results:*\n`$ticker` : Company Name\n`" + ("-" * 21) + "`\n"
|
||||
for _, query in queries.iterrows():
|
||||
desc = query["description"]
|
||||
reply += "`" + desc.replace(": ", "` : ") + "\n"
|
||||
print(reply)
|
||||
update.message.reply_text(
|
||||
text=reply,
|
||||
parse_mode=telegram.ParseMode.MARKDOWN,
|
||||
@@ -466,28 +468,23 @@ def inline_query(update: Update, context: CallbackContext):
|
||||
Handles inline query. Searches by looking if query is contained
|
||||
in the symbol and returns matches in alphabetical order.
|
||||
"""
|
||||
info(f"Inline command ran by {update.message.chat.username}")
|
||||
# info(f"Inline command ran by {update.message.chat.username}")
|
||||
info(f"Query: {update.inline_query.query}")
|
||||
matches = s.inline_search(update.inline_query.query)[:5]
|
||||
|
||||
symbols = " ".join([match[1].split(":")[0] for match in matches])
|
||||
prices = s.batch_price_reply(s.find_symbols(symbols))
|
||||
matches = s.inline_search(update.inline_query.query)
|
||||
|
||||
results = []
|
||||
for match, price in zip(matches, prices):
|
||||
try:
|
||||
results.append(
|
||||
InlineQueryResultArticle(
|
||||
str(uuid4()),
|
||||
title=match[1],
|
||||
input_message_content=InputTextMessageContent(
|
||||
price, parse_mode=telegram.ParseMode.MARKDOWN
|
||||
),
|
||||
)
|
||||
for _, row in matches.iterrows():
|
||||
print(row)
|
||||
results.append(
|
||||
InlineQueryResultArticle(
|
||||
str(uuid4()),
|
||||
title=row["description"],
|
||||
input_message_content=InputTextMessageContent(
|
||||
row["price_reply"], parse_mode=telegram.ParseMode.MARKDOWN
|
||||
),
|
||||
)
|
||||
except TypeError:
|
||||
warning(f"{match} caused error in inline query.")
|
||||
pass
|
||||
)
|
||||
|
||||
if len(results) == 5:
|
||||
update.inline_query.answer(results)
|
||||
@@ -568,6 +565,7 @@ def main():
|
||||
dp.add_handler(CommandHandler("random", rand_pick))
|
||||
dp.add_handler(CommandHandler("donate", donate))
|
||||
dp.add_handler(CommandHandler("status", status))
|
||||
dp.add_handler(CommandHandler("inline", inline_query))
|
||||
|
||||
# Charting can be slow so they run async.
|
||||
dp.add_handler(CommandHandler("intra", intra, run_async=True))
|
||||
|
Reference in New Issue
Block a user