class TelegramWebhookBot(TelegramStatusBot): name = "TelegramWebhookBot" def __init__(self, token, *args, certificate=None, **kwargs): TelegramStatusBot.__init__(self, token, *args, **kwargs) self._certificate = certificate self.webhook = WebhookResource('telegram' + token) self.webhook.setServiceParent(self) @defer.inlineCallbacks def startService(self): yield super().startService() url = bytes2unicode(self.master.config.buildbotURL) if not url.endswith('/'): url += '/' yield self.set_webhook(url + self.webhook.path, self._certificate) def process_webhook(self, request): update = self.get_update(request) return self.process_update(update) def get_update(self, request): content = request.content.read() content = bytes2unicode(content) content_type = request.getHeader(b'Content-Type') content_type = bytes2unicode(content_type) if content_type is not None and \ content_type.startswith('application/json'): update = json.loads(content) else: raise ValueError('Unknown content type: {}'.format(content_type)) return update @defer.inlineCallbacks def set_webhook(self, url, certificate=None): if not certificate: self.log("Setting up webhook to: {}".format(url)) yield self.post('/setWebhook', json=dict(url=url)) else: self.log( "Setting up webhook to: {} (custom certificate)".format(url)) certificate = io.BytesIO(unicode2bytes(certificate)) yield self.post('/setWebhook', data=dict(url=url), files=dict(certificate=certificate))
def __init__(self, token, *args, certificate=None, **kwargs): TelegramStatusBot.__init__(self, token, *args, **kwargs) self._certificate = certificate self.webhook = WebhookResource('telegram' + token) self.webhook.setServiceParent(self)