How To Create Self Hosted RSS to Telegram Bot

Note: You can also follow the updated version of this script on https://github.com/xilverkamui/rss-to-telegram-bot-gcr

I can provide you with a basic implementation of a Telegram bot in Python that posts new RSS feed items to Telegram, and guide you on how to wrap it as a Docker container and host it on Google Cloud Run.

Here's the code for the Telegram bot:

import os
import feedparser
import requests
from telegram import Bot

bot_token = os.environ.get('BOT_TOKEN')
chat_ids = os.environ.get('CHAT_IDS').split(',')
rss_urls = os.environ.get('RSS_URLS').split(',')

def post_new_feed_items():
    last_fetched_item_id = os.environ.get('LAST_FETCHED_ITEM_ID')

    for rss_url in rss_urls:
        feed = feedparser.parse(rss_url)
        for entry in feed.entries:
            item_id = entry.get('id')
            if item_id == last_fetched_item_id:
                break
            
            title = entry.get('title')
            description = entry.get('description')
            link = entry.get('link')

            message = f'{title}\n{description}\n{link}'
            for chat_id in chat_ids:
                bot = Bot(token=bot_token)
                bot.send_message(chat_id=chat_id, text=message, parse_mode='HTML')

            # Update last fetched item ID
            os.environ['LAST_FETCHED_ITEM_ID'] = item_id

# Run the bot
post_new_feed_items()
  

 To wrap this bot as a Docker container, you need to create a Dockerfile with the following content:

FROM python:3.9

WORKDIR /app

COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt

COPY . .

CMD [ "python", "bot.py" ]   
 

Create a requirements.txt file containing the required Python dependencies:

    python-telegram-bot 
    feedparser
 

Now, you can build the Docker image and push it to a container registry. For example, using the Google Cloud Container Registry:

  
# Build the Docker image
docker build -t gcr.io//telegram-bot .

# Push the image to Google Cloud Container Registry
docker push gcr.io//telegram-bot
  

Finally, deploy the container to Google Cloud Run:

gcloud run deploy telegram-bot \
  --image gcr.io//telegram-bot \
  --platform managed \
  --region  \
  --allow-unauthenticated
 

Replace <project-id> with your Google Cloud project ID and <region> with the desired region.

Once deployed, you can configure the required environment variables (BOT_TOKEN, CHAT_IDS, RSS_URLS, and LAST_FETCHED_ITEM_ID) in the Google Cloud Run environment variables section. Make sure to update these values with the appropriate tokens, chat IDs, RSS URLs, and initial last fetched item ID.

The bot will periodically check the RSS feeds and post new items to the specified Telegram chat IDs. The last fetched item ID will be stored in the Google Cloud Run environment variable to ensure only new items are posted.

Please note that you might need to add error handling, logging, and additional features based on your specific requirements.