diff --git a/AUTHORS.rst b/AUTHORS.rst index cc898d1fc..c21c55e0e 100644 --- a/AUTHORS.rst +++ b/AUTHORS.rst @@ -20,6 +20,7 @@ The following wonderful people contributed directly or indirectly to this projec - `naveenvhegde `_ - `njittam `_ - `Noam Meltzer `_ +- `Oleg Shlyazhko `_ - `Rahiel Kasim `_ - `sooyhwang `_ - `wjt `_ diff --git a/telegram/utils/botan.py b/telegram/utils/botan.py new file mode 100644 index 000000000..1c6b774c3 --- /dev/null +++ b/telegram/utils/botan.py @@ -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 diff --git a/tests/test_botan.py b/tests/test_botan.py new file mode 100644 index 000000000..1206b8bab --- /dev/null +++ b/tests/test_botan.py @@ -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()