Skip to content
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
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,17 @@ GOTIFY_BASE_URL = '<your gotify server url>'
GOTIFY_TOKEN = '<your application key here>'
```

### Telegram
Create a bot via telegrams 'BotFather'. You will need the token from this and your own telegram chat id.

Edit the script and enter your bot token and chat_id.

```python
# Telegram settings
TELEGRAM_BOT_TOKEN = '<your telegram bot token>'
TELEGRAM_CHAT_ID = '<your telegram chat id>'
```

## Dependencies

The script uses the Feedparser module to parse RSS feed information, so you'll have to install it first.
Expand All @@ -59,6 +70,12 @@ It also uses Requests to send the notification to the Pushbullet API.
```python
pip install requests
```

For the telegram version you will also need the python telegram bot module.
```python
pip install python-telegram-bot
```

## Usage

The easiest way to run this scrip continuously is to use ```nohup```
Expand Down
78 changes: 78 additions & 0 deletions rpilocator_telegram.py
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)
Copy link

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) ?

Copy link
Author

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.

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)