From 9de46d4f69b2e0c8bb2a8c1bc2fee904fc340f53 Mon Sep 17 00:00:00 2001 From: francocruces Date: Sat, 28 Oct 2017 01:20:06 -0300 Subject: [PATCH] Send long messages as a private message. --- AZScrapper.py | 15 +++++++++------ Config.py | 6 +++++- README.md | 6 +++--- main.py | 11 +++++++++-- 4 files changed, 26 insertions(+), 12 deletions(-) diff --git a/AZScrapper.py b/AZScrapper.py index f69f131..ae2e9dc 100644 --- a/AZScrapper.py +++ b/AZScrapper.py @@ -14,7 +14,7 @@ from telepot.namedtuple import InlineQueryResultArticle, InputTextMessageContent, InlineKeyboardButton, \ InlineKeyboardMarkup -from Config import MAX_RESULTS +from Config import MAX_RESULTS, NO_RESULTS_ALERT __author__ = "Franco Cruces Ayala" @@ -40,7 +40,8 @@ def get_lyrics_as_inline_keyboard(query): else: return [InlineQueryResultArticle( id=0, title="NO RESULTS", input_message_content=InputTextMessageContent( - message_text="No results :c") + message_text=NO_RESULTS_ALERT, + parse_mode="Markdown") )] @@ -56,7 +57,7 @@ def get_search_result(query): panels = BeautifulSoup(page.content, 'lxml').find_all("div", class_='panel') print("Search results loaded: " + str(query)) result = [] - count = 1 + count = 0 for i in panels: if "Song" in i.div.text: for j in i.table.find_all("tr"): @@ -68,6 +69,7 @@ def get_search_result(query): 'artist': song[1].get_text(), 'url': j.a.get('href') }) + count = count + 1 except urllib.error.HTTPError: print("Page unreachable") sys.exit(1) @@ -128,7 +130,6 @@ def get_lyric_body_from_query(given_id, query): if len(j.td.find_all("b")) == 2: if count == int(given_id): return get_lyric_body(j.a.get('href')) - count += 1 return "Couldn't fetch lyrics" @@ -148,11 +149,13 @@ def get_lyrics(query): title=title, description="", input_message_content=InputTextMessageContent( - message_text="Won't load too many lyrics at once" # This method is deprecated + message_text="Won't load too many lyrics at once", # This method is deprecated + parse_mode="Markdown" ))) if len(result) == 0: result.append(InlineQueryResultArticle( id=0, title="NO RESULTS", input_message_content=InputTextMessageContent( - message_text="No results :c") + message_text=NO_RESULTS_ALERT, + parse_mode="Markdown") )) return result diff --git a/Config.py b/Config.py index 3d33861..d7be1d2 100644 --- a/Config.py +++ b/Config.py @@ -3,4 +3,8 @@ """ MAX_RETRIES = 20 -MAX_RESULTS = 15 # Max amount of results to show. Set to -1 for no limit. +MAX_RESULTS = 7 # Max amount of results to show. Set to -1 for no limit. +MAX_MESSAGE_SIZE = 4096 # A message cannot contain more characters +MESSAGE_TOO_LONG_ALERT = "_Message too long. Sending through private message.\n Please make sure you have started a " \ + "conversation with this bot first via PM @EasyLyricsBot._" +NO_RESULTS_ALERT = "_No results :c_" diff --git a/README.md b/README.md index 9fe5fdb..32443ee 100644 --- a/README.md +++ b/README.md @@ -1,12 +1,12 @@ # Easy Lyrics Bot -This telegram bot works as in inline mode and retrieves lyrics for the given query. +This is an inline mode bot and retrieves lyrics from AZ Lyrics for the given query. ## How it works This is an inline bot, that means that can be called from any chat by simply typing @EasyLyricsBot. -After @EasyLyricsBot you should write your lyric search, which could contain artist's name for higher precision. An +After @EasyLyricsBot you should write your lyric search, which could contain the artist's name for higher precision. An inline result will pop up showing the amount of results loaded. By selecting the article result, a message will be sent with an inline keyboard, where every button is a search result. -After selecting a result, the message will be edited so it no longer shows the inline keyboard and the text will have +After selecting a result, the message will be edited so it no longer shows the inline keyboard and the text will be the found lyrics. \ No newline at end of file diff --git a/main.py b/main.py index e86afc2..7557c6b 100644 --- a/main.py +++ b/main.py @@ -8,6 +8,7 @@ from telepot.aio.delegate import per_inline_from_id, create_open, pave_event_space, intercept_callback_query_origin from telepot.aio.helper import InlineUserHandler, AnswererMixin, InterceptCallbackQueryMixin from telepot.aio.loop import MessageLoop +from Config import MAX_MESSAGE_SIZE, MESSAGE_TOO_LONG_ALERT from AZScrapper import get_lyrics_as_inline_keyboard, get_lyric_body_from_id from __TOKEN__ import TOKEN # Replace with your own token. Provided by BotFather @@ -51,8 +52,14 @@ def on_chosen_inline_result(self, msg): async def on_callback_query(self, msg): print(msg) in_id = msg['inline_message_id'] - await self.bot.editMessageText(str(in_id), get_lyric_body_from_id(msg['data'])) - # TODO: Catch MESSAGE_TOO_LONG error + lyrics = get_lyric_body_from_id(msg['data']) + if len(lyrics) <= MAX_MESSAGE_SIZE: + await self.bot.editMessageText(str(in_id), lyrics) + else: + await self.bot.editMessageText(str(in_id), MESSAGE_TOO_LONG_ALERT, parse_mode="Markdown") + while lyrics != "": + await self.bot.sendMessage(msg['from']['id'], lyrics[:MAX_MESSAGE_SIZE]) + lyrics = lyrics[MAX_MESSAGE_SIZE:] # ASYNC MAIN