Skip to content

Instantly share code, notes, and snippets.

@ljmocic
Last active April 21, 2024 11:39
Show Gist options
  • Star 16 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save ljmocic/ec013451ff8bbb51f308d79975d7fdb4 to your computer and use it in GitHub Desktop.
Save ljmocic/ec013451ff8bbb51f308d79975d7fdb4 to your computer and use it in GitHub Desktop.
# Read more about setting it up
# https://medium.com/@ljmocic/make-telegram-bot-for-notifying-about-new-rss-feed-items-4cfbcc37f4fd
from datetime import timedelta, datetime
from dateutil import parser
from pprint import pprint
from time import sleep
import requests
import feedparser
BOT_TOKEN = ''
CHANNEL_ID = '' # @bot_channel_name
FEED_URL = '' # https://something.com/feeds/rss.xml
def send_message(message):
requests.get(f'https://api.telegram.org/bot{BOT_TOKEN}/sendMessage?chat_id={CHANNEL_ID}&text={message}')
def main():
rss_feed = feedparser.parse(FEED_URL)
for entry in rss_feed.entries:
parsed_date = parser.parse(entry.published)
parsed_date = (parsed_date - timedelta(hours=8)).replace(tzinfo=None) # remove timezone offset
now_date = datetime.utcnow()
published_20_minutes_ago = now_date - parsed_date < timedelta(minutes=20)
if published_20_minutes_ago:
send_message(entry.links[0].href)
print(entry.links[0].href)
if __name__ == "__main__":
while(True):
main()
sleep(20 * 60)
Copy link

ghost commented Feb 27, 2021

Hello. I would like to ask. How can parse multiple FEED_URL. Like your code, it only parse one FEED_URL

@oCaioGuerras
Copy link

Hello. I would like to ask. How can parse multiple FEED_URL. Like your code, it only parse one FEED_URL ²

@aligalehban
Copy link

create function for each feed and call them when u need

@MagiCsito
Copy link

Once it send feeds the first time , will it skip to send the same items again in the second round and only send the new ones?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment