Skip to content

Commit

Permalink
Update for better logging
Browse files Browse the repository at this point in the history
  • Loading branch information
Francesco Scarlato (Jungmann) committed Feb 8, 2016
1 parent 90a1b32 commit 641180b
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 17 deletions.
2 changes: 1 addition & 1 deletion django_telegrambot/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
__version__ = '0.1.6'
__version__ = '0.1.7b'
default_app_config = 'django_telegrambot.apps.DjangoTelegramBot'
43 changes: 29 additions & 14 deletions django_telegrambot/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
from django.conf import settings
import importlib
import telegram
import os.path

import logging
logger = logging.getLogger(__name__)

class DjangoTelegramBot(AppConfig):

Expand Down Expand Up @@ -51,28 +55,41 @@ def ready(self):
if DjangoTelegramBot.ready_run: return
DjangoTelegramBot.ready_run = True

if not hasattr(settings, 'TELEGRAM_BOT_TOKENS'): return
if not hasattr(settings, 'TELEGRAM_BOT_TOKENS'):
logger.debug('Required TELEGRAM_BOT_TOKENS missing in settings')
return
tokens = settings.TELEGRAM_BOT_TOKENS

if not hasattr(settings, 'TELEGRAM_WEBHOOK_SITE'): return
if not hasattr(settings, 'TELEGRAM_WEBHOOK_SITE'):
logger.debug('Required TELEGRAM_WEBHOOK_SITE missing in settings')
return
webhook_site = settings.TELEGRAM_WEBHOOK_SITE

if not hasattr(settings, 'TELEGRAM_WEBHOOK_BASE'): return
if not hasattr(settings, 'TELEGRAM_WEBHOOK_BASE'):
logger.debug('Required TELEGRAM_WEBHOOK_BASE missing in settings')
return
webhook_base = settings.TELEGRAM_WEBHOOK_BASE

use_certificate = False
if hasattr(settings, 'TELEGRAM_WEBHOOK_CERTIFICATE'):
CERT = settings.TELEGRAM_WEBHOOK_CERTIFICATE
if(os.path.exists(CERT)):
use_certificate = True
logger.debug('TELEGRAM_WEBHOOK_CERTIFICATE found in {}'.format(CERT))
else:
logger.error('TELEGRAM_WEBHOOK_CERTIFICATE not found in {} '.format(CERT))


for index, token in enumerate(tokens):

bot = telegram.Bot(token=token)
hookurl = '{}{}/{}/'.format(webhook_site,webhook_base, token)
if hasattr(settings, 'TELEGRAM_WEBHOOK_CERTIFICATE'):
if (use_certificate):
setted = bot.setWebhook(hookurl, certificate=open(CERT,'rb'))
else:
setted = bot.setWebhook(hookurl, certificate=None)

print ('Telegram Bot <{}> setting webhook [ {} ] : {}'.format(bot.username,hookurl,setted))
logger.debug('Telegram Bot <{}> setting webhook [ {} ] : {}'.format(bot.username,hookurl,setted))

DjangoTelegramBot.dispatchers.append(telegram.Dispatcher(bot, None))
DjangoTelegramBot.bots.append(bot)
Expand All @@ -82,19 +99,21 @@ def ready(self):
#per compatibilità salvo il primo bot nella proprietà DjangoTelegramBot.dispatcher
if index==0:
DjangoTelegramBot.dispatcher = DjangoTelegramBot.dispatchers[0]
logger.debug('Telegram Bot <{}> set as default bot'.format(bot.username))


def module_exists(module_name, method_name, execute):
try:
#m = __import__(module_name).telegrambot
m = importlib.import_module(module_name)
if execute and hasattr(m, method_name):
print ('Run {}.main()'.format(module_name))
logger.debug('Run {}.main()'.format(module_name))
getattr(m,method_name)()
else:
print ('Run {}'.format(module_name))
logger.debug('Run {}'.format(module_name))

except ImportError:
except ImportError as er:
logger.debug('{} : {}'.format(module_name, repr(er)))
return False

return True
Expand All @@ -103,9 +122,5 @@ def module_exists(module_name, method_name, execute):
for app in settings.INSTALLED_APPS:
module_name = '{}.telegrambot'.format( app )
if module_exists(module_name, 'main', True):
print ('Loaded {}'.format( module_name))


#import telegrambot
#telegrambot.main() # in alternativa si potrebbe imporre l'uso di un main in telegrambot.py

logger.debug('Loaded {}'.format(module_name))

4 changes: 2 additions & 2 deletions django_telegrambot/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def webhook (request, bot_token):

try:
data = json.loads(request.body.decode("utf-8"))
logger.info(data)
logger.debug(data)
except:
logger.info('Telegram bot receive invalid request' )
return JsonResponse({})
Expand All @@ -35,7 +35,7 @@ def webhook (request, bot_token):
try:
update = telegram.Update.de_json(data)
dispatcher.processUpdate(update)
logger.debug('Processed Update: {} with context {}'.format(update, context))
logger.debug('Processed Update: {}'.format(update))
# Dispatch any errors
except TelegramError as te:
logger.warn("Error was raised while processing Update.")
Expand Down

0 comments on commit 641180b

Please sign in to comment.