Idea Implementation #1 β Automating Daily Quiz Delivery to Telegram with GitHub Actions
Telegram Group: https://t.me/parottasalna
Github Repo: https://github.com/syedjaferk/daily_quiz_sender
In this blog, Iβll walk you through the journey of transitioning from a PythonAnywhere server to GitHub Actions for automating the delivery of daily quizzes to a Telegram group https://t.me/parottasalna . This implementation highlights the benefits of GitHub Actions to run a cronjob.
Problem Statement
I wanted to send a daily quiz to a Telegram group to keep members engaged and learning. Initially, I hosted the solution on a PythonAnywhere server.
Some of the limitations are,
- Only one cron job is allowed for a free account.
- For every x days, i need to visit the dashboard and reactive my account to make it work.
Recently, started learning the Github Actions . So thought of leveraging the schedule mechanism in it for my usecase.
Key Features of My Solution
- Automated Scheduling: GitHub Actions triggers the script daily at a specified time.
- Secure Secrets Management: Sensitive information like Telegram bot tokens and chat IDs are stored securely using GitHub Secrets.
- Serverless Architecture: No server maintenance; everything runs on GitHubβs infrastructure.
Implementation Overview
I am not explaining the code here. Please checkout the repo https://github.com/syedjaferk/daily_quiz_sender
The core components of the implementation include
- A Python script (populate_questions.py) to create questions and store each question in the respective directory.
- A Python script (runner.py) which takes telegram_bot_url, chat_id and category as input to read the correct question from the category directory and send the message.
- A GitHub Actions workflow to automate execution.
Workflow Yaml File
name: Docker Quiz Sender on: schedule: - cron: "30 4 * * *" workflow_dispatch: jobs: run-python: runs-on: ubuntu-latest steps: - name: Checkout code uses: actions/checkout@v3 - name: Set up Python uses: actions/setup-python@v4 with: python-version: '3.10' - name: Install dependencies run: | python -m pip install --upgrade pip if [ -f requirements.txt ]; then pip install -r requirements.txt; fi - name: Run script with secrets0 run: python runner.py --telegram_url ${{ secrets.TELEGRAM_BOT_URL }} --chat_id ${{ secrets.CHAT_ID }} --category docker
Benefits of the New Implementation
- Cost-Effectiveness: No server costs thanks to GitHubβs free tier for public repositories.
- Reliability: GitHub Actions ensures the script runs on schedule without manual intervention.
- Security: GitHub Secrets securely manages sensitive credentials.
- Centralization: All code, configuration, and automation reside in a single repository.
Lessons Learned
- Environment Variables: Using environment variables for secrets keeps the code clean and secure.
- Error Handling: Adding error-handling mechanisms in the script helps debug issues effectively.
- Time Zones: GitHub Actions uses UTC; be mindful when scheduling tasks for specific time zones.
Future Enhancements
- Dynamic Quizzes: Automate quiz generation from a database (sqlite3) or external API.
- Logging: Store logs as GitHub Actions artifacts for better debugging and tracking.
- Failure Notifications: Notify via Telegram or email if the workflow fails.
- Extend for other categories
You can check out the project here. Feel free to fork it and adapt it to your needs!