-
Notifications
You must be signed in to change notification settings - Fork 22
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
Telegram script #4
Open
andy71
wants to merge
4
commits into
camerahacks:main
Choose a base branch
from
andy71:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import telegram, sys | ||
import feedparser | ||
import time | ||
import json | ||
|
||
# Feed URL | ||
FEED_URL = 'https://rpilocator.com/feed/' | ||
|
||
# Telegram settings | ||
TELEGRAM_BOT_TOKEN = '<your telegram bot token>' | ||
TELEGRAM_CHAT_ID = '<your telegram chat id>' | ||
|
||
# Customize the message title | ||
MESSAGE_TITLE = 'xlocator Stock Alert' | ||
|
||
# User Agent | ||
USER_AGENT = 'xlocator feed alert' | ||
|
||
# Create the message body | ||
def formatMessage(entry): | ||
message = [ | ||
f"<b><u>{MESSAGE_TITLE}</u></b>", | ||
f"", | ||
f"{entry.title}", | ||
f"", | ||
f"{entry.link}", | ||
] | ||
|
||
message = '\n'.join(message) | ||
|
||
return message | ||
|
||
# Telegram Nachricht senden | ||
def sendMessage (message): | ||
try: | ||
bot = telegram.Bot(token=TELEGRAM_BOT_TOKEN) | ||
except Exception as err: | ||
print('Unhandled exception while creating telegram bot object: %s' % err, file = sys.stderr) | ||
return False | ||
try: | ||
result_msg = bot.sendMessage(chat_id=TELEGRAM_CHAT_ID, text=message, parse_mode='HTML') | ||
return isinstance(result_msg, telegram.message.Message) | ||
except Exception as err: | ||
print('Unhandled exception while sending telegram message: %s' % err, file = sys.stderr) | ||
return False | ||
|
||
# Set control to blank list | ||
control = [] | ||
|
||
# Fetch the feed | ||
f = feedparser.parse(FEED_URL, agent=USER_AGENT) | ||
|
||
# If there are entries in the feed, add entry guid to the control variable | ||
if f.entries: | ||
for entries in f.entries: | ||
control.append(entries.id) | ||
|
||
#Only wait 30 seconds after initial run. | ||
time.sleep(30) | ||
|
||
while True: | ||
# Fetch the feed again, and again, and again... | ||
f = feedparser.parse(FEED_URL, agent=USER_AGENT) | ||
|
||
# Compare feed entries to control list. | ||
# If there are new entries, send a message/push | ||
# and add the new entry to control variable | ||
for entries in f.entries: | ||
if entries.id not in control: | ||
|
||
message = formatMessage(entries) | ||
|
||
sendMessage(message) | ||
|
||
# Add entry guid to the control variable | ||
control.append(entries.id) | ||
|
||
time.sleep(59) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Quick question, any reason why you return anything within
sendMessage(X)
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're right. They are from another project from me. Damn copy&paste ;-)
I have removed the returns.