Creating a Telegram bot that posts daily top 5 Twitter trends in a private group involves several steps.
We'll use Python and some libraries for this project.
Creating a Telegram bot: Create a new bot by talking to the BotFather on Telegram. Follow the instructions, and you'll receive a bot token. Save this token for later.
Creating a Twitter Developer account:
Go to Twitter Developer and sign up for a developer account if you don't have one. Create a new project and generate your API key, API key secret, Access token, and Access token secret. Save these for later. Setting up the Python project:
- Create a new folder for your project. Inside the project folder, create a new Python file, e.g., telegram_twitter_bot.py.
Install the required libraries by running
pip install python-telegram-bot tweepy python-dotenv.
Create a .env file in the project folder and add the following variables:
TELEGRAM_BOT_TOKEN=your_telegram_bot_token
TWITTER_API_KEY=your_twitter_api_key
TWITTER_API_SECRET=your_twitter_api_secret
TWITTER_ACCESS_TOKEN=your_twitter_access_token
TWITTER_ACCESS_SECRET=your_twitter_access_secret
TELEGRAM_CHAT_ID=your_telegram_private_group_chat_id
Writing the code: In your telegram_twitter_bot.py, add the following code:
import os
import tweepy
from telegram import Update, ParseMode
from telegram.ext import Updater, CommandHandler, CallbackContext
from dotenv import load_dotenv
load_dotenv()
# Set up Twitter API
auth = tweepy.OAuthHandler(os.environ["TWITTER_API_KEY"], os.environ["TWITTER_API_SECRET"])
auth.set_access_token(os.environ["TWITTER_ACCESS_TOKEN"], os.environ["TWITTER_ACCESS_SECRET"])
api = tweepy.API(auth)
# Set up Telegram API
updater = Updater(os.environ["TELEGRAM_BOT_TOKEN"])
dispatcher = updater.dispatcher
# Function to get top 5 Twitter trends
def get_top_5_trends(woeid):
trends = api.trends_place(woeid)[0]["trends"]
sorted_trends = sorted(trends, key=lambda x: x["tweet_volume"], reverse=True)[:5]
return [t["name"] for t in sorted_trends]
# Function to handle the /trends command
def trends(update: Update, context: CallbackContext):
woeid = 23424848 # Replace with your desired country's WOEID
trends = get_top_5_trends(woeid)
message = "Top 5 Twitter trends:\n\n" + "\n".join(trends)
context.bot.send_message(chat_id=update.effective_chat.id, text=message, parse_mode=ParseMode.HTML)
dispatcher.add_handler(CommandHandler("trends", trends))
# Schedule daily trends post
def daily_trends(context: CallbackContext):
woeid = 23424848 # Replace with your desired country's WOEID
trends = get_top_5_trends(woeid)
message = "Daily top 5 Twitter trends:\n\n" + "\n".join(trends)
context.bot.send_message(chat_id=os.environ["TELEGRAM_CHAT_ID"], text=message, parse_mode=ParseMode.HTML)
job_queue = updater.job_queue
job_queue.run_daily(daily_trends, time=datetime.time(hour=13, minute=0))
# Start the bot
updater.start_polling()
updater.idle()
-
Replace the woeid variable in the trends and daily_trends functions with the WOEID of your desired country.
-
Deploying the bot: You can deploy the bot on a platform like Vercel using GitHub Actions to automate the deployment process. Follow these steps: Push your project to a GitHub repository. Add your tokens and secrets to the repository secrets. Create a .github/workflows/deploy.yml file in your repository with the following content:
name: Deploy
on:
push:
branches:
- main
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v2
- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: 3.x
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install python-telegram-bot tweepy python-dotenv
- name: Deploy to Vercel
uses: vercel/action@19.0.1
with:
vercel-token: ${{ secrets.VERCEL_TOKEN }}
vercel-org-id: ${{ secrets.VERCEL_ORG_ID }}
vercel-project-id: ${{ secrets.VERCEL_PROJECT_ID }}
github-token: ${{ secrets.GITHUB_TOKEN }}
vercel-args: '--prod'
Go to Vercel and create a new project for your bot. Set the environment variables using the values from your .env file. Running the bot: Once the bot is deployed, you can add it to your Telegram private group using the bot's username. After adding the bot, type /trends to test it. It will post the top 5 Twitter trends daily at 1 PM. And that's it! You've created a Telegram bot that posts daily top 5 Twitter trends in your private group.