Skip to content

Commit

Permalink
Send long messages as a private message.
Browse files Browse the repository at this point in the history
  • Loading branch information
francocruces committed Oct 28, 2017
1 parent ee2c1ee commit 9de46d4
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 12 deletions.
15 changes: 9 additions & 6 deletions AZScrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -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")
)]


Expand All @@ -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"):
Expand All @@ -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)
Expand Down Expand Up @@ -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"

Expand All @@ -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
6 changes: 5 additions & 1 deletion Config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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_"
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -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.
11 changes: 9 additions & 2 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 9de46d4

Please sign in to comment.