mirror of
https://github.com/python-telegram-bot/python-telegram-bot.git
synced 2024-11-22 15:17:00 +01:00
Merge pull request #159 from ollmer/botan_integration
Botan integration
This commit is contained in:
commit
77f00e386c
3 changed files with 118 additions and 0 deletions
|
@ -20,6 +20,7 @@ The following wonderful people contributed directly or indirectly to this projec
|
|||
- `naveenvhegde <https://github.com/naveenvhegde>`_
|
||||
- `njittam <https://github.com/njittam>`_
|
||||
- `Noam Meltzer <https://github.com/tsnoam>`_
|
||||
- `Oleg Shlyazhko <https://github.com/ollmer>`_
|
||||
- `Rahiel Kasim <https://github.com/rahiel>`_
|
||||
- `sooyhwang <https://github.com/sooyhwang>`_
|
||||
- `wjt <https://github.com/wjt>`_
|
||||
|
|
55
telegram/utils/botan.py
Normal file
55
telegram/utils/botan.py
Normal file
|
@ -0,0 +1,55 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
import json
|
||||
import logging
|
||||
from telegram import NullHandler
|
||||
|
||||
try:
|
||||
from urllib.request import urlopen, Request
|
||||
from urllib.parse import quote
|
||||
from urllib.error import URLError, HTTPError
|
||||
except ImportError:
|
||||
from urllib2 import urlopen, Request
|
||||
from urllib import quote
|
||||
from urllib2 import URLError, HTTPError
|
||||
|
||||
H = NullHandler()
|
||||
logging.getLogger(__name__).addHandler(H)
|
||||
|
||||
|
||||
class Botan(object):
|
||||
"""This class helps to send incoming events in your botan analytics account.
|
||||
See more: https://github.com/botanio/sdk#botan-sdk"""
|
||||
|
||||
token = ''
|
||||
url_template = 'https://api.botan.io/track?token={token}' \
|
||||
'&uid={uid}&name={name}&src=python-telegram-bot'
|
||||
|
||||
def __init__(self, token):
|
||||
self.token = token
|
||||
self.logger = logging.getLogger(__name__)
|
||||
|
||||
def track(self, message, event_name='event'):
|
||||
try:
|
||||
uid = message.chat_id
|
||||
except AttributeError:
|
||||
self.logger.warn('No chat_id in message')
|
||||
return False
|
||||
data = json.dumps(message.__dict__)
|
||||
try:
|
||||
url = self.url_template.format(token=str(self.token),
|
||||
uid=str(uid),
|
||||
name=quote(event_name))
|
||||
request = Request(url,
|
||||
data=data.encode(),
|
||||
headers={'Content-Type': 'application/json'})
|
||||
urlopen(request)
|
||||
return True
|
||||
except HTTPError as error:
|
||||
self.logger.warn('Botan track error ' +
|
||||
str(error.code) +
|
||||
':' + error.read().decode('utf-8'))
|
||||
return False
|
||||
except URLError as error:
|
||||
self.logger.warn('Botan track error ' + str(error.reason))
|
||||
return False
|
62
tests/test_botan.py
Normal file
62
tests/test_botan.py
Normal file
|
@ -0,0 +1,62 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
"""This module contains a object that represents Tests for Botan analytics integration"""
|
||||
|
||||
import os
|
||||
import unittest
|
||||
import sys
|
||||
sys.path.append('.')
|
||||
|
||||
from telegram.utils.botan import Botan
|
||||
from tests.base import BaseTest
|
||||
|
||||
|
||||
class MessageMock(object):
|
||||
chat_id = None
|
||||
|
||||
def __init__(self, chat_id):
|
||||
self.chat_id = chat_id
|
||||
|
||||
|
||||
class BotanTest(BaseTest, unittest.TestCase):
|
||||
"""This object represents Tests for Botan analytics integration."""
|
||||
|
||||
token = os.environ.get('BOTAN_TOKEN')
|
||||
|
||||
def test_track(self):
|
||||
"""Test sending event to botan"""
|
||||
print('Test sending event to botan')
|
||||
botan = Botan(self.token)
|
||||
message = MessageMock(self._chat_id)
|
||||
result = botan.track(message, 'named event')
|
||||
self.assertTrue(result)
|
||||
|
||||
def test_track_fail(self):
|
||||
"""Test fail when sending event to botan"""
|
||||
print('Test fail when sending event to botan')
|
||||
botan = Botan(self.token)
|
||||
botan.url_template = 'https://api.botan.io/traccc?token={token}&uid={uid}&name={name}'
|
||||
message = MessageMock(self._chat_id)
|
||||
result = botan.track(message, 'named event')
|
||||
self.assertFalse(result)
|
||||
|
||||
def test_wrong_message(self):
|
||||
"""Test sending wrong message"""
|
||||
print('Test sending wrong message')
|
||||
botan = Botan(self.token)
|
||||
message = MessageMock(self._chat_id)
|
||||
message = delattr(message, 'chat_id')
|
||||
result = botan.track(message, 'named event')
|
||||
self.assertFalse(result)
|
||||
|
||||
def test_wrong_endpoint(self):
|
||||
"""Test wrong endpoint"""
|
||||
print('Test wrong endpoint')
|
||||
botan = Botan(self.token)
|
||||
botan.url_template = 'https://api.botaaaaan.io/traccc?token={token}&uid={uid}&name={name}'
|
||||
message = MessageMock(self._chat_id)
|
||||
result = botan.track(message, 'named event')
|
||||
self.assertFalse(result)
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
Loading…
Reference in a new issue