mirror of
https://github.com/python-telegram-bot/python-telegram-bot.git
synced 2024-12-22 14:35:00 +01:00
Merge pull request #305 from python-telegram-bot/move-botan
move botan from utils to contrib
This commit is contained in:
commit
f31bd91673
5 changed files with 58 additions and 53 deletions
7
docs/source/telegram.contrib.botan.rst
Normal file
7
docs/source/telegram.contrib.botan.rst
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
telegram.contrib.botan module
|
||||||
|
=============================
|
||||||
|
|
||||||
|
.. automodule:: telegram.contrib.botan
|
||||||
|
:members:
|
||||||
|
:undoc-members:
|
||||||
|
:show-inheritance:
|
0
telegram/contrib/__init__.py
Normal file
0
telegram/contrib/__init__.py
Normal file
46
telegram/contrib/botan.py
Normal file
46
telegram/contrib/botan.py
Normal file
|
@ -0,0 +1,46 @@
|
||||||
|
import logging
|
||||||
|
from telegram import NullHandler
|
||||||
|
|
||||||
|
from future.moves.urllib.parse import quote
|
||||||
|
from future.moves.urllib.error import HTTPError, URLError
|
||||||
|
from future.moves.urllib.request import urlopen, Request
|
||||||
|
|
||||||
|
logging.getLogger(__name__).addHandler(NullHandler())
|
||||||
|
|
||||||
|
|
||||||
|
class Botan(object):
|
||||||
|
"""This class helps to send incoming events to 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 = message.to_json()
|
||||||
|
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
|
|
@ -1,52 +1,4 @@
|
||||||
#!/usr/bin/env python
|
from .deprecate import deprecate
|
||||||
|
from telegram.contrib.botan import Botan as Bo
|
||||||
|
|
||||||
import logging
|
Botan = deprecate(Bo, 'telegram.utils.botan', 'telegram.contrib.botan')
|
||||||
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
|
|
||||||
|
|
||||||
logging.getLogger(__name__).addHandler(NullHandler())
|
|
||||||
|
|
||||||
|
|
||||||
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 = message.to_json()
|
|
||||||
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
|
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
#!/usr/bin/env python
|
#!/usr/bin/env python
|
||||||
"""This module contains a object that represents Tests for Botan analytics integration"""
|
"""This module contains an object that represents Tests for Botan analytics integration"""
|
||||||
|
|
||||||
import sys
|
import sys
|
||||||
import unittest
|
import unittest
|
||||||
|
@ -9,7 +9,7 @@ from flaky import flaky
|
||||||
|
|
||||||
sys.path.append('.')
|
sys.path.append('.')
|
||||||
|
|
||||||
from telegram.utils.botan import Botan
|
from telegram.contrib.botan import Botan
|
||||||
from tests.base import BaseTest
|
from tests.base import BaseTest
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue