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

Implementing support for multiple Telegram chat_ids #124

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
8 changes: 5 additions & 3 deletions pystemon.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,10 @@ storage:
telegram:
storage-classname: TelegramStorage
enable: no # Enable this alerting engine
save: yes
token: 0 # see https://core.telegram.org/bots/api#authorizing-your-bot
chat-id: 0
chat-ids:
- 0

email:
alert: no # Enable/disable email alerts
Expand Down Expand Up @@ -178,7 +180,7 @@ site:
download-url: 'http://paste.org.ru/?{id}'
pastie-classname: PastiePasteOrgRu
throttling: 5000

kpaste.net:
enable: no
archive-url: 'http://kpaste.net/'
Expand Down Expand Up @@ -206,7 +208,7 @@ site:
archive-regex: '<td><a href="(\d+)" title='
download-url: 'http://pastebin.gr/paste.php?download&id={id}'
throttling: 5000

pastebin.pl:
enable: no
archive-url: 'https://pastebin.pl/lists'
Expand Down
16 changes: 8 additions & 8 deletions pystemon/storage/telegramstorage.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@

import logging.handlers
import requests
from pystemon.storage import PastieStorage

logger = logging.getLogger('pystemon')

class TelegramStorage(PastieStorage):

def __init_storage__(self, **kwargs):
self.token = kwargs.get('token')
self.chat_id = kwargs.get('chat-id')
self.chat_ids = kwargs.get('chat-ids')

def __save_pastie__(self, pastie):
if pastie.matched:
Expand All @@ -26,10 +27,9 @@ def __save_pastie__(self, pastie):
'''.format(site=pastie.site.name, url=pastie.public_url, matches=pastie.matches_to_regex(), content=pastie.pastie_content.decode('utf8'))

url = 'https://api.telegram.org/bot{0}/sendMessage'.format(self.token)
try:
logger.debug('Sending message to telegram {} for pastie_id {}'.format(url, pastie.id))
requests.post(url, data={'chat_id': self.chat_id, 'text': message})
except Exception as e:
logger.warning("Failed to alert through telegram: {0}".format(e))


for chat_id in self.chat_ids:
try:
logger.debug('Sending message to telegram {} for pastie_id {}'.format(url, pastie.id))
requests.post(url, data={'chat_id': chat_id, 'text': message})
except Exception as e:
logger.warning("Failed to alert through telegram: {0}".format(e))