Skip to content

Commit

Permalink
Merge pull request #84 from ilstreltsov/telegram_bot
Browse files Browse the repository at this point in the history
Telegram bot
  • Loading branch information
gotlium authored Jan 25, 2017
2 parents 6ab28ad + 6e31dff commit 092aca6
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 6 deletions.
8 changes: 8 additions & 0 deletions dbmail/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,14 @@ def send_db_push(*args, **kwargs):
return db_sender(*args, **kwargs)


def send_db_bot(*args, **kwargs):
from dbmail.defaults import BACKEND, BOT_QUEUE

kwargs['backend'] = kwargs.pop('backend', BACKEND['bot'])
kwargs['queue'] = kwargs.pop('queue', BOT_QUEUE)
return db_sender(*args, **kwargs)


def send_db_subscription(*args, **kwargs):
from dbmail.defaults import (
SUBSCRIPTION_QUEUE, SEND_MAX_TIME,
Expand Down
27 changes: 27 additions & 0 deletions dbmail/backends/bot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# -*- encoding: utf-8 -*-

from dbmail.defaults import BOT_PROVIDER, DEFAULT_BOT_FROM
from dbmail.backends.sms import (
Sender as SenderBase,
SenderDebug as SenderDebugBase
)
from dbmail.utils import clean_html
from dbmail import import_module


class Sender(SenderBase):
provider = BOT_PROVIDER
default_from = DEFAULT_BOT_FROM

def _get_recipient_list(self, recipient):
return self._email_to_list(recipient)

def _send(self):
module = import_module(self._provider)
message = clean_html(self._message)
for _id in self._recipient_list:
module.send(_id, message, _from=self._from_email, **self._kwargs)


class SenderDebug(SenderDebugBase):
pass
6 changes: 6 additions & 0 deletions dbmail/defaults.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ def get_settings(key, default):
PUSH_QUEUE = get_settings('DB_MAILER_PUSH_QUEUE', CELERY_QUEUE)
SMS_QUEUE = get_settings('DB_MAILER_SMS_QUEUE', CELERY_QUEUE)
TTS_QUEUE = get_settings('DB_MAILER_TTS_QUEUE', CELERY_QUEUE)
BOT_QUEUE = get_settings('DB_MAILER_BOT_QUEUE', CELERY_QUEUE)
SIGNALS_QUEUE = get_settings('DB_MAILER_SIGNALS_QUEUE', CELERY_QUEUE)
SIGNALS_MAIL_QUEUE = get_settings('DB_MAILER_SIGNALS_MAIL_QUEUE', CELERY_QUEUE)
SUBSCRIPTION_QUEUE = get_settings('DB_MAILER_SUBSCRIPTION_QUEUE', CELERY_QUEUE)
Expand All @@ -32,6 +33,7 @@ def get_settings(key, default):
DEFAULT_FROM_EMAIL = get_settings('DB_MAILER_DEFAULT_FROM_EMAIL', None)
DEFAULT_SMS_FROM = get_settings('DB_MAILER_DEFAULT_SMS_FROM', None)
DEFAULT_PUSH_FROM = get_settings('DB_MAILER_DEFAULT_PUSH_FROM', None)
DEFAULT_BOT_FROM = get_settings('DB_MAILER_DEFAULT_BOT_FROM', None)
DEFAULT_PRIORITY = get_settings('DB_MAILER_DEFAULT_PRIORITY', 6)
TEMPLATES_PER_PAGE = get_settings('DB_MAILER_TEMPLATES_PER_PAGE', 20)
SEND_RETRY = get_settings('DB_MAILER_SEND_RETRY', 3)
Expand Down Expand Up @@ -92,13 +94,15 @@ def get_settings(key, default):
'tts': 'dbmail.backends.tts',
'sms': 'dbmail.backends.sms',
'push': 'dbmail.backends.push',
'bot': 'dbmail.backends.bot',
})
_BACKEND = {v: k for k, v in BACKEND.items()}
BACKENDS_MODEL_CHOICES = get_settings('DB_MAILER_BACKENDS_MODEL_CHOICES', (
(BACKEND.get('mail'), _('MailBox')),
(BACKEND.get('push'), _('Push')),
(BACKEND.get('sms'), _('SMS')),
(BACKEND.get('tts'), _('TTS')),
(BACKEND.get('bot'), _('BOT')),
))

SMS_PROVIDER = get_settings(
Expand All @@ -107,6 +111,8 @@ def get_settings(key, default):
'DB_MAILER_TTS_PROVIDER', 'dbmail.providers.nexmo.tts')
PUSH_PROVIDER = get_settings(
'DB_MAILER_PUSH_PROVIDER', 'dbmail.providers.prowl.push')
BOT_PROVIDER = get_settings(
'DB_MAILER_BOT_PROVIDER', 'dbmail.providers.telegram.bot')
MAIL_PROVIDER = get_settings('DB_MAILER_MAIL_PROVIDER', None)

SAFARI_PUSH_PATH = get_settings(
Expand Down
1 change: 1 addition & 0 deletions dbmail/providers/telegram/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# coding=utf-8;
29 changes: 29 additions & 0 deletions dbmail/providers/telegram/bot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# -*- encoding: utf-8 -*-

import telepot
from django.conf import settings


def send(to, message, **kwargs):
"""
SITE: https://github.com/nickoala/telepot
TELEGRAM API: https://core.telegram.org/bots/api
Installation:
pip install 'telepot>=10.4'
"""

available_kwargs_keys = [
'parse_mode',
'disable_web_page_preview',
'disable_notification',
'reply_to_message_id',
'reply_markup'
]

available_kwargs = {
k: v for k, v in kwargs.iteritems() if k in available_kwargs_keys
}

bot = telepot.Bot(settings.TELEGRAM_BOT_TOKEN)
return bot.sendMessage(to, message, **available_kwargs)
12 changes: 6 additions & 6 deletions dbmail/urls.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
# -*- encoding: utf-8 -*-

from django.conf.urls import patterns, url
from django.conf.urls import url

from dbmail.views import (
send_by_dbmail, mail_read_tracker,
SafariPushPackagesView, SafariSubscriptionView, SafariLogView,
PushSubscriptionView
)

urlpatterns = patterns(
'dbmail.views',
url(r'^api/', 'send_by_dbmail', name='db-mail-api'),
urlpatterns = [
url(r'^api/', send_by_dbmail, name='db-mail-api'),
url(r'^mail_read_tracker/(.*?)/$',
'mail_read_tracker', name='db-mail-tracker'),
mail_read_tracker, name='db-mail-tracker'),

url(r'^safari/v(?P<version>[0-9]{1})/pushPackages/(?P<site_pid>[.\w-]+)/?',
SafariPushPackagesView.as_view()),
Expand All @@ -24,4 +24,4 @@
PushSubscriptionView.as_view(), name='push-subscribe'),
url(r'^(?P<reg_type>web-push|mobile)/unsubscribe/',
PushSubscriptionView.as_view(), name='push-unsubscribe'),
)
]

0 comments on commit 092aca6

Please sign in to comment.