Add barebones to better tgbot

Also send tg spam from ohlhafv challenges!
This commit is contained in:
okalintu
2019-02-08 18:26:06 +02:00
parent 0f0389b8ff
commit 8a8751ce66
4 changed files with 83 additions and 11 deletions
+37
View File
@@ -0,0 +1,37 @@
'''
A telegram bot api for whatever purposes.
TODO: kaehmy app is definitely not correct place for this
'''
import requests
import logging
from django.conf import settings
from kaehmy.models import TelegramChannel
class TelegramBot:
'''
A telegram bot api for whatever purposes
Currently only able to broadcast stuff to all registered
channels using broadcast method.
'''
def __init__(self, api_token=None):
self.api_token = api_token or settings.TELEGRAM_BOT_TOKEN
self.send_message_url = f'https://api.telegram.org/bot{self.api_token}/sendMessage'
def broadcast(self, message):
channels_ids = TelegramChannel.objects.values_list("channel_id", flat=True)
for id_ in channels_ids:
self.send_message(id_, message)
def send_message(self, channel_id, message):
'''
Send message to a chat with given channel_id
'''
data = {
'chat_id': channel_id,
'text': message,
'parse_mode': 'Markdown'
}
resp = requests.post(self.send_message_url, json=data)
logging.debug(resp.content)
+8
View File
@@ -0,0 +1,8 @@
Uusi Ohlhafv haaste!
====================
Haastaja: {{ challenge.challenger }}
Uhri: {{ challenge.victim }}
Sarja: {{ challenge.get_series_display }}
Terveiset: {{ challenge.message }}
Käy kurkkaamassa muutkin haasteet osoitteessa: {{ url }}
+8
View File
@@ -19,6 +19,7 @@ from ohlhafv.models import OhlhafvChallenge
from ohlhafv.forms import OhlhafvForm from ohlhafv.forms import OhlhafvForm
from ohlhafv.tables import OhlhafvTable from ohlhafv.tables import OhlhafvTable
from webapp.utils import send_email from webapp.utils import send_email
from kaehmy.tgbot import TelegramBot
@require_http_methods(["GET"]) @require_http_methods(["GET"])
@@ -48,6 +49,13 @@ def ohlhafv_submit(request, *args, **kwargs):
} }
) )
send_email(email, subject, message) send_email(email, subject, message)
tg_message = render_to_string(
'ohlhafv:tgmsg.tpl', {
'challenge': challenge,
'url': url})
bot = TelegramBot()
bot.broadcast(tg_message)
logging.debug( logging.debug(
'Sent ohlhafv email to recipient <{}>'.format(email)) 'Sent ohlhafv email to recipient <{}>'.format(email))
else: else:
+30 -11
View File
@@ -886,7 +886,8 @@
}, },
"ansi-regex": { "ansi-regex": {
"version": "2.1.1", "version": "2.1.1",
"bundled": true "bundled": true,
"optional": true
}, },
"aproba": { "aproba": {
"version": "1.2.0", "version": "1.2.0",
@@ -904,11 +905,13 @@
}, },
"balanced-match": { "balanced-match": {
"version": "1.0.0", "version": "1.0.0",
"bundled": true "bundled": true,
"optional": true
}, },
"brace-expansion": { "brace-expansion": {
"version": "1.1.11", "version": "1.1.11",
"bundled": true, "bundled": true,
"optional": true,
"requires": { "requires": {
"balanced-match": "^1.0.0", "balanced-match": "^1.0.0",
"concat-map": "0.0.1" "concat-map": "0.0.1"
@@ -921,15 +924,18 @@
}, },
"code-point-at": { "code-point-at": {
"version": "1.1.0", "version": "1.1.0",
"bundled": true "bundled": true,
"optional": true
}, },
"concat-map": { "concat-map": {
"version": "0.0.1", "version": "0.0.1",
"bundled": true "bundled": true,
"optional": true
}, },
"console-control-strings": { "console-control-strings": {
"version": "1.1.0", "version": "1.1.0",
"bundled": true "bundled": true,
"optional": true
}, },
"core-util-is": { "core-util-is": {
"version": "1.0.2", "version": "1.0.2",
@@ -1032,7 +1038,8 @@
}, },
"inherits": { "inherits": {
"version": "2.0.3", "version": "2.0.3",
"bundled": true "bundled": true,
"optional": true
}, },
"ini": { "ini": {
"version": "1.3.5", "version": "1.3.5",
@@ -1042,6 +1049,7 @@
"is-fullwidth-code-point": { "is-fullwidth-code-point": {
"version": "1.0.0", "version": "1.0.0",
"bundled": true, "bundled": true,
"optional": true,
"requires": { "requires": {
"number-is-nan": "^1.0.0" "number-is-nan": "^1.0.0"
} }
@@ -1054,17 +1062,20 @@
"minimatch": { "minimatch": {
"version": "3.0.4", "version": "3.0.4",
"bundled": true, "bundled": true,
"optional": true,
"requires": { "requires": {
"brace-expansion": "^1.1.7" "brace-expansion": "^1.1.7"
} }
}, },
"minimist": { "minimist": {
"version": "0.0.8", "version": "0.0.8",
"bundled": true "bundled": true,
"optional": true
}, },
"minipass": { "minipass": {
"version": "2.2.4", "version": "2.2.4",
"bundled": true, "bundled": true,
"optional": true,
"requires": { "requires": {
"safe-buffer": "^5.1.1", "safe-buffer": "^5.1.1",
"yallist": "^3.0.0" "yallist": "^3.0.0"
@@ -1081,6 +1092,7 @@
"mkdirp": { "mkdirp": {
"version": "0.5.1", "version": "0.5.1",
"bundled": true, "bundled": true,
"optional": true,
"requires": { "requires": {
"minimist": "0.0.8" "minimist": "0.0.8"
} }
@@ -1153,7 +1165,8 @@
}, },
"number-is-nan": { "number-is-nan": {
"version": "1.0.1", "version": "1.0.1",
"bundled": true "bundled": true,
"optional": true
}, },
"object-assign": { "object-assign": {
"version": "4.1.1", "version": "4.1.1",
@@ -1163,6 +1176,7 @@
"once": { "once": {
"version": "1.4.0", "version": "1.4.0",
"bundled": true, "bundled": true,
"optional": true,
"requires": { "requires": {
"wrappy": "1" "wrappy": "1"
} }
@@ -1238,7 +1252,8 @@
}, },
"safe-buffer": { "safe-buffer": {
"version": "5.1.1", "version": "5.1.1",
"bundled": true "bundled": true,
"optional": true
}, },
"safer-buffer": { "safer-buffer": {
"version": "2.1.2", "version": "2.1.2",
@@ -1268,6 +1283,7 @@
"string-width": { "string-width": {
"version": "1.0.2", "version": "1.0.2",
"bundled": true, "bundled": true,
"optional": true,
"requires": { "requires": {
"code-point-at": "^1.0.0", "code-point-at": "^1.0.0",
"is-fullwidth-code-point": "^1.0.0", "is-fullwidth-code-point": "^1.0.0",
@@ -1285,6 +1301,7 @@
"strip-ansi": { "strip-ansi": {
"version": "3.0.1", "version": "3.0.1",
"bundled": true, "bundled": true,
"optional": true,
"requires": { "requires": {
"ansi-regex": "^2.0.0" "ansi-regex": "^2.0.0"
} }
@@ -1323,11 +1340,13 @@
}, },
"wrappy": { "wrappy": {
"version": "1.0.2", "version": "1.0.2",
"bundled": true "bundled": true,
"optional": true
}, },
"yallist": { "yallist": {
"version": "3.0.2", "version": "3.0.2",
"bundled": true "bundled": true,
"optional": true
} }
} }
}, },