Exemple #1
0
def set_api(sender, instance, **kwargs):
    #  set bot api if not yet
    if not instance._bot:
        instance._bot = BotAPI(instance.token)

    # set webhook
    url = None
    cert = None
    if instance.enabled:
        webhook = reverse('telegrambot:webhook', kwargs={'token': instance.token})        
        from django.contrib.sites.models import Site
        current_site = Site.objects.get_current()
        url = 'https://' + current_site.domain + webhook   
    if instance.ssl_certificate:
        cert = instance.ssl_certificate.open()
    instance._bot.setWebhook(webhook_url=url, 
                             certificate=cert)
    logger.info("Success: Webhook url %s for bot %s set" % (url, str(instance)))
    
    #  complete  Bot instance with api data
    if not instance.user_api:
        bot_api = instance._bot.getMe()
        user_api, _ = User.objects.get_or_create(**bot_api.to_dict())
        instance.user_api = user_api
        instance.save()
        logger.info("Success: Bot api info for bot %s set" % str(instance))
Exemple #2
0
def set_api(sender, instance, **kwargs):
    #  Always reset the _bot instance after save, in case the token changes.
    instance._bot = BotAPI(instance.token)

    # set webhook
    url = None
    cert = None
    if instance.enabled:
        webhook = reverse('telegrambot:webhook',
                          kwargs={'token': instance.token})
        from django.contrib.sites.models import Site
        current_site = Site.objects.get_current()
        if instance.https_port is None:
            url = 'https://' + current_site.domain + webhook
        else:
            url = 'https://' + current_site.domain + ':' + str(
                instance.https_port) + webhook
    if instance.ssl_certificate:
        instance.ssl_certificate.open()
        cert = instance.ssl_certificate

    instance._bot.setWebhook(webhook_url=url, certificate=cert)
    logger.info("Success: Webhook url %s for bot %s set" %
                (url, str(instance)))

    #  complete  Bot instance with api data
    if not instance.user_api:
        bot_api = instance._bot.getMe()

        botdict = bot_api.to_dict()
        modelfields = [f.name for f in User._meta.get_fields()]
        params = {k: botdict[k] for k in botdict.keys() if k in modelfields}
        user_api, _ = User.objects.get_or_create(**params)
        instance.user_api = user_api

        # Prevent signal recursion, and save.
        post_save.disconnect(set_api, sender=sender)
        instance.save()
        post_save.connect(set_api, sender=sender)

        logger.info("Success: Bot api info for bot %s set" % str(instance))
Exemple #3
0
def set_api(sender, instance, **kwargs):
    #  set bot api if not yet
    if not instance._bot:
        instance._bot = BotAPI(instance.token)

    # set webhook
    url = None
    cert = None
    if instance.enabled:
        webhook = reverse('telegrambot:webhook',
                          kwargs={'token': instance.token})
        from django.contrib.sites.models import Site
        current_site = Site.objects.get_current()
        url = 'https://' + current_site.domain + webhook
    if instance.ssl_certificate:
        cert = instance.ssl_certificate.open()
    instance._bot.setWebhook(webhook_url=url, certificate=cert)
    logger.info("Success: Webhook url %s for bot %s set" %
                (url, str(instance)))

    #  complete  Bot instance with api data
    if not instance.user_api:
        bot_api = instance._bot.getMe()
        user_dict = bot_api.to_dict()
        user_fields = [
            field.name
            for field in User._meta.get_fields(include_parents=False,
                                               include_hidden=False)
        ]
        safe_dict = {
            key: value
            for key, value in user_dict.items() if key in user_fields
        }
        user_api, _ = User.objects.get_or_create(**safe_dict)
        instance.user_api = user_api
        instance.save()
        logger.info("Success: Bot api info for bot %s set" % str(instance))
Exemple #4
0
 def __init__(self, *args, **kwargs):
     super(Bot, self).__init__(*args, **kwargs)
     self._bot = None
     if self.token:
         self._bot = BotAPI(self.token)