Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added /help message, updated deps #19

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
122 changes: 80 additions & 42 deletions bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
import os
import sys
import emoji
import telegram
import telegram.ext
from telegram import Update
from telegram.constants import ParseMode
from telegram.ext import ApplicationBuilder, CommandHandler, ContextTypes

READER_DB_PATH = "/usr/src/app/reader/db.sqlite"
BOT_TOKEN = os.environ["BOT_TOKEN"]
Expand All @@ -22,26 +23,26 @@


async def error_handler(
update: telegram.Update, context: telegram.ext.CallbackContext.DEFAULT_TYPE
update: Update, context: ContextTypes.DEFAULT_TYPE
) -> None:
"""Error handler.

Args:
update (telegram.Update): Object representing the incoming update from Telegram.
context (telegram.ext.CallbackContext.DEFAULT_TYPE): Object representing the callback context.
update (Update): Object representing the incoming update from Telegram.
context (ContextTypes.DEFAULT_TYPE): Object representing the callback context.
"""
logger.error(f"{CROSS_MARK_EMOJI} {context.error}: {update}")


async def check_feeds(context: telegram.ext.CallbackContext.DEFAULT_TYPE) -> None:
async def check_feeds(context: ContextTypes.DEFAULT_TYPE) -> None:
"""Check for feed updates.

Args:
context (telegram.ext.CallbackContext.DEFAULT_TYPE): Object representing the callback context.
context (ContextTypes.DEFAULT_TYPE): Object representing the callback context.
"""

async def mark_entries_as_read(
reader: r.Reader, feed_title: str, entries: List[r.Entry]
reader: r.Reader, feed_title: str, entries: List[r.Entry]
) -> None:
"""Mark entries as read.

Expand Down Expand Up @@ -96,14 +97,14 @@ async def mark_entries_as_read(


async def send_feed_entries(
context: telegram.ext.CallbackContext.DEFAULT_TYPE,
feed_title: str,
entries: List[r.Entry],
context: ContextTypes.DEFAULT_TYPE,
feed_title: str,
entries: List[r.Entry],
) -> None:
"""Send feed entries.

Args:
context (telegram.ext.CallbackContext.DEFAULT_TYPE): Object representing the callback context.
context (ContextTypes.DEFAULT_TYPE): Object representing the callback context.
feed_title (str): The title of the feed.
entries (List[r.Entry]): Feed entries to send.
"""
Expand All @@ -123,13 +124,13 @@ async def send_feed_entries(


async def add_feeds(
update: telegram.Update, context: telegram.ext.CallbackContext.DEFAULT_TYPE
update: Update, context: ContextTypes.DEFAULT_TYPE
) -> None:
"""Add feeds.

Args:
update (telegram.Update): Object representing the incoming update from Telegram.
context (telegram.ext.CallbackContext.DEFAULT_TYPE): Object representing the callback context.
update (Update): Object representing the incoming update from Telegram.
context (ContextTypes.DEFAULT_TYPE): Object representing the callback context.

Command Args:
url (str): The URL of the feed to add.
Expand All @@ -151,13 +152,13 @@ async def add_feeds(


async def remove_feeds(
update: telegram.Update, context: telegram.ext.CallbackContext.DEFAULT_TYPE
update: Update, context: ContextTypes.DEFAULT_TYPE
) -> None:
"""Remove feeds.

Args:
update (telegram.Update): Object representing the incoming update from Telegram.
context (telegram.ext.CallbackContext.DEFAULT_TYPE): Object representing the callback context.
update (Update): Object representing the incoming update from Telegram.
context (ContextTypes.DEFAULT_TYPE): Object representing the callback context.

Command Args:
url (str): The URL of the feed to remove.
Expand All @@ -179,13 +180,13 @@ async def remove_feeds(


async def change_interval(
update: telegram.Update, context: telegram.ext.CallbackContext.DEFAULT_TYPE
update: Update, context: ContextTypes.DEFAULT_TYPE
) -> None:
"""Change the feed update interval.

Args:
update (telegram.Update): Object representing the incoming update from Telegram.
context (telegram.ext.CallbackContext.DEFAULT_TYPE): Object representing the callback context.
update (Update): Object representing the incoming update from Telegram.
context (ContextTypes.DEFAULT_TYPE): Object representing the callback context.

Command Args:
interval (int): The interval in seconds to check for feed updates.
Expand All @@ -212,13 +213,13 @@ async def change_interval(


async def show_feeds(
update: telegram.Update, context: telegram.ext.CallbackContext.DEFAULT_TYPE
update: Update, context: ContextTypes.DEFAULT_TYPE
) -> None:
"""Show feeds.

Args:
update (telegram.Update): Object representing the incoming update from Telegram.
context (telegram.ext.CallbackContext.DEFAULT_TYPE): Object representing the callback context.
update (Update): Object representing the incoming update from Telegram.
context (ContextTypes.DEFAULT_TYPE): Object representing the callback context.
"""
context_logger = logger.bind(function="show_feeds")
context_logger.info(f"{CHECK_MARK_EMOJI} Showing feeds.")
Expand All @@ -235,13 +236,13 @@ async def show_feeds(


async def show_job(
update: telegram.Update, context: telegram.ext.CallbackContext.DEFAULT_TYPE
update: Update, context: ContextTypes.DEFAULT_TYPE
) -> None:
"""Show the datetime of the next feed update job.

Args:
update (telegram.Update): Object representing the incoming update from Telegram.
context (telegram.ext.CallbackContext.DEFAULT_TYPE): Object representing the callback context.
update (Update): Object representing the incoming update from Telegram.
context (ContextTypes.DEFAULT_TYPE): Object representing the callback context.
"""
context_logger = logger.bind(function="show_job")
context_logger.info(f"{CHECK_MARK_EMOJI} Showing feed(s) job.")
Expand All @@ -255,16 +256,49 @@ async def show_job(
)


async def help_msg(
update: Update, context: ContextTypes.DEFAULT_TYPE
) -> None:
"""Send help message

When someone does /help, the help message is sent from here.

Args:
update (Update): Object representing the incoming update from Telegram.
context (ContextTypes.DEFAULT_TYPE): Object representing the callback context.

Returns: None

"""
context_logger = logger.bind(function="help")
context_logger.info(f"{CHECK_MARK_EMOJI} Sending help message.")
await context.bot.send_message(
chat_id=update.effective_chat.id,
text=
"""
Here are the things that you can do.
/start <interval> - Start reading feeds
/addfeeds <url> ... - Add feeds.
/removefeeds <url> ... - Remove feeds..
/changeinterval <interval> - Change the feed update interval.
/showfeeds - Show feeds.
/showjob - Show the datetime of the next feed update job..
/help - This message.
""",
parse_mode=ParseMode.MARKDOWN
)


async def start(
update: telegram.Update, context: telegram.ext.CallbackContext.DEFAULT_TYPE
update: Update, context: ContextTypes.DEFAULT_TYPE
) -> None:
"""Start reading feeds.

Starts the background job to read feeds on an interval.

Args:
update (telegram.Update): Object representing the incoming update from Telegram.
context (telegram.ext.CallbackContext.DEFAULT_TYPE): Object representing the callback context.
update (Update): Object representing the incoming update from Telegram.
context (ContextTypes.DEFAULT_TYPE): Object representing the callback context.

Command Args:
interval (int): The interval in seconds to check for feed updates.
Expand All @@ -277,18 +311,22 @@ async def start(
)
context.job_queue.run_repeating(check_feeds, interval=interval, first=1)
context_logger.info(f"{CHECK_MARK_EMOJI} Started feed(s) job.")
await update.message.reply_text(f"{CHECK_MARK_EMOJI} Started feed(s) job.")
await context.bot.send_message(chat_id=update.effective_chat.id,
text=f"{CHECK_MARK_EMOJI} Started feed(s) job.")
except IndexError:
context_logger.error(f"{CROSS_MARK_EMOJI} Interval missing.")
await update.message.reply_text(f"{CROSS_MARK_EMOJI} Interval missing.")


app = telegram.ext.ApplicationBuilder().token(BOT_TOKEN).build()
app.add_error_handler(error_handler)
app.add_handler(telegram.ext.CommandHandler("start", start))
app.add_handler(telegram.ext.CommandHandler("addfeeds", add_feeds))
app.add_handler(telegram.ext.CommandHandler("removefeeds", remove_feeds))
app.add_handler(telegram.ext.CommandHandler("changeinterval", change_interval))
app.add_handler(telegram.ext.CommandHandler("showfeeds", show_feeds))
app.add_handler(telegram.ext.CommandHandler("showjob", show_job))
await update.message.reply_text(
f"{CROSS_MARK_EMOJI} Interval missing. Please do /help.",
parse_mode=ParseMode.MARKDOWN)


app = ApplicationBuilder().token(BOT_TOKEN).build()
app.add_error_handler(callback=error_handler)
app.add_handler(CommandHandler("start", start))
app.add_handler(CommandHandler("addfeeds", add_feeds))
app.add_handler(CommandHandler("removefeeds", remove_feeds))
app.add_handler(CommandHandler("changeinterval", change_interval))
app.add_handler(CommandHandler("showfeeds", show_feeds))
app.add_handler(CommandHandler("showjob", show_job))
app.add_handler(CommandHandler("help", help_msg))
app.run_polling()
65 changes: 34 additions & 31 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,31 +1,34 @@
anyio==3.6.1; python_full_version >= "3.6.2" and python_version >= "3.7"
apscheduler==3.9.1; python_version >= "3.7" and python_full_version < "3.0.0" or python_full_version >= "3.5.0" and python_version < "4" and python_version >= "3.7"
beautifulsoup4==4.11.1; python_full_version >= "3.6.0" and python_version >= "3.8"
cachetools==5.0.0; python_version >= "3.7" and python_version < "4.0"
certifi==2022.6.15; python_version >= "3.8" and python_version < "4"
charset-normalizer==2.1.0; python_version >= "3.8" and python_version < "4" and python_full_version >= "3.6.0"
colorama==0.4.5; python_version >= "3.5" and python_full_version < "3.0.0" and sys_platform == "win32" or sys_platform == "win32" and python_version >= "3.5" and python_full_version >= "3.5.0"
emoji==1.7.0
feedparser==6.0.10; python_version >= "3.8"
h11==0.12.0; python_version >= "3.7"
httpcore==0.14.7; python_version >= "3.7"
httpx==0.22.0; python_version >= "3.7"
idna==3.3; python_version >= "3.8" and python_version < "4" and python_full_version >= "3.6.2"
iso8601==1.0.2; python_full_version >= "3.6.2" and python_version < "4.0" and python_version >= "3.8"
loguru==0.6.0; python_version >= "3.5"
python-telegram-bot==20.0a0; python_version >= "3.7"
pytz-deprecation-shim==0.1.0.post0; python_version >= "3.7" and python_full_version < "3.0.0" or python_full_version >= "3.6.0" and python_version < "4" and python_version >= "3.7"
pytz==2022.1; python_version >= "3.7" and python_full_version < "3.0.0" or python_full_version >= "3.5.0" and python_version < "4" and python_version >= "3.7"
reader==2.15; python_version >= "3.8"
requests==2.28.1; python_version >= "3.8" and python_version < "4"
rfc3986==1.5.0; python_version >= "3.7"
sgmllib3k==1.0.0; python_version >= "3.8"
six==1.16.0; python_version >= "3.7" and python_full_version < "3.0.0" or python_full_version >= "3.5.0" and python_version < "4" and python_version >= "3.7"
sniffio==1.2.0; python_full_version >= "3.6.2" and python_version >= "3.7"
soupsieve==2.3.2.post1; python_full_version >= "3.6.0" and python_version >= "3.8"
tornado==6.2; python_version >= "3.7"
typing-extensions==4.3.0; python_version >= "3.8"
tzdata==2022.1; python_version >= "3.7" and python_full_version < "3.0.0" and platform_system == "Windows" or python_full_version >= "3.6.0" and python_version < "4" and python_version >= "3.7" and platform_system == "Windows"
tzlocal==4.2; python_version >= "3.7" and python_full_version < "3.0.0" or python_full_version >= "3.5.0" and python_version < "4" and python_version >= "3.7"
urllib3==1.26.10; python_version >= "3.8" and python_full_version < "3.0.0" and python_version < "4" or python_full_version >= "3.6.0" and python_version < "4" and python_version >= "3.8"
win32-setctime==1.1.0; sys_platform == "win32" and python_version >= "3.5"
anyio==4.3.0
APScheduler==3.10.4
beautifulsoup4==4.12.3
cachetools==5.3.3
certifi==2024.2.2
charset-normalizer==3.3.2
colorama==0.4.6
emoji==2.11.1
feedparser==6.0.11
h11==0.14.0
httpcore==1.0.5
httpx==0.27.0
idna==3.7
iso8601==2.1.0
loguru==0.7.2
MarkupSafe==2.1.5
python-telegram-bot==21.1.1
pytz==2024.1
pytz-deprecation-shim==0.1.0.post0
reader==3.12
requests==2.31.0
rfc3986==2.0.0
setuptools==69.5.1
sgmllib3k==1.0.0
six==1.16.0
sniffio==1.3.1
soupsieve==2.5
tornado==6.4
typing_extensions==4.11.0
tzdata==2024.1
tzlocal==5.2
urllib3==2.2.1
Werkzeug==3.0.3
win32-setctime==1.1.0