-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: follow spot via inline button (#122)
* feat: follow spot via inline button * feat: follow spot via inline button * applying recommendations * fix: codefactor * fix: always shows manual reactions * removed trailing whitespace * test: give priority to delete anonymous comment instead of follow one --------- Co-authored-by: Neos <[email protected]> Co-authored-by: Stefano Borzì <[email protected]>
- Loading branch information
1 parent
df9a904
commit b005aed
Showing
6 changed files
with
142 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
"""Detect Comment on a post in the comment group""" | ||
from telegram import Update | ||
from telegram.ext import CallbackContext | ||
from modules.data import Config | ||
from modules.utils import EventInfo | ||
from modules.data.db_manager import DbManager | ||
|
||
|
||
def follow_spot_comment(update: Update, context: CallbackContext): | ||
"""Handles a new comment on a post in the comment group. | ||
Checks if someone is following the post, and sends them an update in case. | ||
Args: | ||
update: update event | ||
context: context passed by the handler | ||
""" | ||
info = EventInfo.from_message(update, context) | ||
|
||
if info.chat_id == Config.meme_get('channel_group_id'): | ||
# Get the spot's message_id | ||
reply_to_message_id = info.message.message_thread_id | ||
# Get a list of users who are following the spot | ||
result = DbManager.select_from( | ||
table_name = "user_follow", | ||
select = "user_id, private_message_id", | ||
where = "message_id = %s", | ||
where_args = (reply_to_message_id, ) | ||
) | ||
|
||
# Send them an update about the new comment | ||
for user in result: | ||
# Avoid sending if it's made by the same user | ||
if not user['user_id'] == info.message.from_user.id: | ||
info.message.copy(chat_id=user['user_id'], reply_to_message_id=user['private_message_id']) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
"Handles callback when the 'Follow Spot' button is clicked." | ||
import logging | ||
from telegram import Update | ||
from telegram.ext import CallbackContext | ||
from telegram.error import Unauthorized | ||
from telegram import InlineKeyboardButton, InlineKeyboardMarkup | ||
from modules.utils import EventInfo | ||
from modules.data import Config | ||
from modules.data.db_manager import DbManager | ||
|
||
|
||
logger = logging.getLogger(__name__) | ||
|
||
def follow_spot_callback(update: Update, context: CallbackContext) -> int: | ||
"""Handles the follow callback. | ||
Args: | ||
update: update event | ||
context: context passed by the handler | ||
Returns: | ||
next state of the conversation | ||
""" | ||
info = EventInfo.from_callback(update, context) | ||
message_id = info.message.reply_to_message.message_id if Config.meme_get('comments') else info.message_id | ||
|
||
# Check if user is already following this spot | ||
result = DbManager.select_from( | ||
table_name = "user_follow", | ||
select = "private_message_id", | ||
where = "user_id = %s and message_id = %s", | ||
where_args = (info.user_id, message_id) | ||
) | ||
|
||
if len(result) > 0: | ||
answer_text = "Non stai più seguendo questo spot." | ||
# Forget the stored data | ||
DbManager.delete_from( | ||
table_name="user_follow", | ||
where="user_id = %s and message_id = %s", | ||
where_args=(info.user_id, message_id) | ||
) | ||
|
||
info.bot.send_message( | ||
chat_id = info.user_id, | ||
text = answer_text, | ||
reply_to_message_id = result[0]['private_message_id'], | ||
disable_notification = True | ||
) | ||
else: | ||
post_url = f"https://t.me/c/{str(info.chat_id).replace('-100', '')}/{info.message_id}" | ||
answer_text = "Stai seguendo questo spot." | ||
try: | ||
# Forward the spot in user's conversation with the bot, so that | ||
# future comments will be sent in response to this forwarded message. | ||
private_message = info.bot.copy_message( | ||
chat_id = info.user_id, | ||
from_chat_id = info.chat_id, | ||
message_id = message_id, | ||
disable_notification = True, | ||
reply_markup = InlineKeyboardMarkup([ | ||
[InlineKeyboardButton("💬 Visualizza thread", url = post_url)] | ||
]) | ||
) | ||
|
||
# Add an explanation to why the message was forwarded? | ||
info.bot.send_message( | ||
chat_id = info.user_id, | ||
text = answer_text, | ||
reply_to_message_id = private_message.message_id | ||
) | ||
except Unauthorized: | ||
info.answer_callback_query(text=f"Assicurati di aver avviato la chat con {Config.settings_get('bot_tag')}") | ||
return -1 | ||
|
||
# Remember the user_id and message_id | ||
DbManager.insert_into( | ||
table_name="user_follow", | ||
columns=("user_id", "message_id", "private_message_id"), | ||
values=(info.user_id, message_id, private_message.message_id) | ||
) | ||
|
||
info.answer_callback_query(text=answer_text) | ||
return -1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -124,3 +124,4 @@ def close_callback(_: None) -> None: | |
text and replyMarkup that make up the reply | ||
""" | ||
return None | ||
# endregion |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters