Add telegram bot
This commit is contained in:
@@ -29,3 +29,4 @@ paho-mqtt==1.3.0
|
||||
django-autocomplete-light==3.2.10
|
||||
six==1.10.0
|
||||
django-suit==0.2.25
|
||||
telepot==12.3
|
||||
|
||||
@@ -49,6 +49,9 @@ EMAIL_HOST_PASSWORD = '<gmail_passu>'
|
||||
DEFAULT_EMAIL_FROM = 'SIK Viestintä <sikviestinta@gmail.com>'
|
||||
ENABLE_AUTOMATIC_EMAILS = False
|
||||
|
||||
# Token for Telegram bot
|
||||
TELEGRAM_BOT_TOKEN = "<BOT_TOKEN>"
|
||||
|
||||
# Database settings
|
||||
# Only uncomment if default settings in base.py are not ok
|
||||
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# Generated by Django 1.11 on 2017-10-16 07:46
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('webapp', '0027_auto_20171012_0037'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.CreateModel(
|
||||
name='TelegramChannel',
|
||||
fields=[
|
||||
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||
('channel_id', models.CharField(max_length=255)),
|
||||
],
|
||||
),
|
||||
]
|
||||
+8
-1
@@ -242,9 +242,16 @@ class OhlhafvChallenge(models.Model):
|
||||
message = models.TextField()
|
||||
|
||||
|
||||
# Telegram channel entry for Kaehmys
|
||||
class TelegramChannel(models.Model):
|
||||
"""Model containing the channel id of a Telegram chat"""
|
||||
|
||||
channel_id = models.CharField(max_length=255)
|
||||
|
||||
|
||||
auditlog.register(Tag)
|
||||
auditlog.register(Feed)
|
||||
auditlog.register(Event)
|
||||
auditlog.register(PresetRole)
|
||||
auditlog.register(Role)
|
||||
auditlog.register(Official)
|
||||
auditlog.register(Official)
|
||||
@@ -0,0 +1,44 @@
|
||||
import sys
|
||||
import asyncio
|
||||
import logging
|
||||
|
||||
import telepot
|
||||
|
||||
from django.conf import settings
|
||||
from webapp.models import TelegramChannel
|
||||
|
||||
TOKEN = settings.TELEGRAM_BOT_TOKEN
|
||||
bot = telepot.Bot(TOKEN)
|
||||
|
||||
logging.getLogger('urllib3.connectionpool').setLevel(logging.WARNING)
|
||||
logging.getLogger('urllib3.util.retry').setLevel(logging.WARNING)
|
||||
|
||||
|
||||
class KaehmyHandler:
|
||||
|
||||
def handle(self, msg):
|
||||
flavor = telepot.flavor(msg)
|
||||
|
||||
if flavor == 'chat':
|
||||
text = msg['text']
|
||||
id = msg['chat']['id']
|
||||
if text == '/start':
|
||||
TelegramChannel.objects.create(channel_id=id)
|
||||
bot.sendMessage(id, 'Moro! Uudet kaehmyt postataan tälle kanavalle.')
|
||||
elif text == '/stop':
|
||||
channels_started = [int(channel.channel_id) for channel in TelegramChannel.objects.all()]
|
||||
if id in channels_started:
|
||||
TelegramChannel.objects.get(channel_id=id).delete()
|
||||
bot.sendMessage(id, 'Lopetetaan kaehmyjen postailu.')
|
||||
|
||||
def announce(self, url, name):
|
||||
channels_started = [channel.channel_id for channel in TelegramChannel.objects.all()]
|
||||
logging.debug('Announcing to {} Telegram channels.'.format(len(channels_started)))
|
||||
for id in channels_started:
|
||||
bot.sendMessage(id, 'Uusi kaehmy/New kaehmy! {} -> {}'.format(name, url))
|
||||
|
||||
|
||||
kaehmy_handler = KaehmyHandler()
|
||||
|
||||
bot.message_loop(kaehmy_handler.handle)
|
||||
logging.debug('Telepot listening...')
|
||||
@@ -8,7 +8,7 @@
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<meta name="description" content="uli uli">
|
||||
<meta name="description" content="Aalto-yliopiston Sähköinsinöörikilta ry">
|
||||
<meta name="author" content="veedeeämkoo">
|
||||
<link rel="stylesheet" href="{% static "css/webapp.css" %}">
|
||||
|
||||
|
||||
+8
-1
@@ -12,6 +12,7 @@ from webapp.models import PresetKaehmyRole, CustomKaehmyRole
|
||||
from webapp.models import OhlhafvChallenge, KaehmyForm
|
||||
from webapp.forms import OhlhafvForm, KaehmyForm_Form, KaehmyCommentForm
|
||||
from webapp.tables import OhlhafvTable
|
||||
from webapp.telegram_bot import kaehmy_handler
|
||||
|
||||
from django.core.mail import send_mail
|
||||
from django.conf import settings
|
||||
@@ -146,15 +147,21 @@ def kaehmy_submit(request, *args, **kwargs):
|
||||
custom_role.save()
|
||||
application.custom_roles.add(custom_role)
|
||||
|
||||
url = 'https://sika.sahkoinsinoorikilta.fi/kaehmy'
|
||||
|
||||
email = form.cleaned_data.get('email', '')
|
||||
name = form.cleaned_data.get('name', 'Anonymous')
|
||||
subject = 'Arwokas kirjattu kirje mahdolliselle tulewalle kiltahenkilölle'
|
||||
body = ('Moikka {}!\r\n\r\nHienoa, että kilta kiinnostaa! Kaehmysi on vastaanotettu.\r\n'
|
||||
'Mahdollisista kommenteista tulee ilmoitus sähköpostitse.\r\n\r\n'
|
||||
'Käy katsomassa kaehmytilanne osoitteessa http://sika.sahkoinsinoorikilta.fi/kaehmy').format(name)
|
||||
'Käy katsomassa kaehmytilanne osoitteessa {}').format(name, url)
|
||||
|
||||
send_email(email, subject, body)
|
||||
logging.debug('Sent kaehmy email to recipient <{}>'.format(email))
|
||||
|
||||
kaehmy_handler.announce(url, name)
|
||||
logging.debug('Sent kaehmy announcement to telegram.')
|
||||
|
||||
else:
|
||||
context = {
|
||||
'errors': form.errors
|
||||
|
||||
Reference in New Issue
Block a user