Skip to content

Commit f6f6ca8

Browse files
committed
Moved listeners to separate file
Removed WorkerThread/ThreadPool. Please use concurrent.futures.ThreadPoolExecutor and friends. Simplified request executor mechanism
1 parent b8d30ed commit f6f6ca8

File tree

13 files changed

+324
-530
lines changed

13 files changed

+324
-530
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@ python:
99
install: "pip install -r requirements.txt"
1010
script:
1111
- python setup.py install
12-
- cd tests && py.test
12+
- py.test

requirements.txt

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
py==1.4.29
2-
pytest==2.7.2
3-
requests==2.7.0
4-
six==1.9.0
5-
wheel==0.24.0
6-
mock==2.0.0
1+
py>=1.4.29
2+
pytest>=2.7.2
3+
requests>=2.7.0
4+
six>=1.9.0
5+
wheel>=0.24.0
6+
mock>=2.0.0
7+
futures>=2.1.3

setup.py

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,31 @@
22
from setuptools import setup
33
from io import open
44

5+
56
def readme():
67
with open('README.rst', encoding='utf-8') as f:
78
return f.read()
89

9-
setup(name='pyTelegramBotAPI',
10-
version='2.1.2',
11-
description='Python Telegram bot api. ',
12-
long_description=readme(),
13-
author='eternnoir',
14-
author_email='[email protected]',
15-
url='https://github.com/eternnoir/pyTelegramBotAPI',
16-
packages=['telebot'],
17-
license='GPL2',
18-
keywords='telegram bot api tools',
19-
install_requires=['requests', 'six'],
20-
classifiers=[
21-
'Development Status :: 5 - Production/Stable',
22-
'Programming Language :: Python :: 2',
23-
'Programming Language :: Python :: 2.6',
24-
'Programming Language :: Python :: 2.7',
25-
'Programming Language :: Python :: 3',
26-
'Environment :: Console',
27-
'License :: OSI Approved :: GNU General Public License v2 (GPLv2)',
28-
]
29-
)
10+
11+
setup(
12+
name='pyTelegramBotAPI',
13+
version='2.1.2',
14+
description='Python Telegram Bot API',
15+
long_description=readme(),
16+
author='eternnoir',
17+
author_email='[email protected]',
18+
url='https://github.com/eternnoir/pyTelegramBotAPI',
19+
packages=['telebot'],
20+
license='GPL2',
21+
keywords='telegram bot api tools',
22+
install_requires=['requests', 'six'],
23+
classifiers=[
24+
'Development Status :: 5 - Production/Stable',
25+
'Programming Language :: Python :: 2',
26+
'Programming Language :: Python :: 2.6',
27+
'Programming Language :: Python :: 2.7',
28+
'Programming Language :: Python :: 3',
29+
'Environment :: Console',
30+
'License :: OSI Approved :: GNU General Public License v2 (GPLv2)',
31+
]
32+
)

telebot/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# -*- coding: utf-8 -*-
2+
from telebot import types
23
from telebot.util import logger
3-
from telebot.telebot_class import TeleBot
4+
from telebot.telebot_class import *
45

56
"""
67
Module : telebot

telebot/apihelper.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
# -*- coding: utf-8 -*-
22

33
import requests
4-
import six
54

65
from telebot import util
76
from telebot import logger
@@ -11,12 +10,7 @@
1110
FILE_URL = "https://api.telegram.org/file/bot{0}/{1}"
1211

1312

14-
class RequestExecutor:
15-
def make_request(self, url, method='get', params=None, files=None, response_type='text'):
16-
raise NotImplementedError()
17-
18-
19-
class RequestExecutorImpl(RequestExecutor):
13+
class RequestExecutorImpl:
2014
DEFAULT_CONNECT_TIMEOUT = 3.5
2115
DEFAULT_READ_TIMEOUT = 9999
2216

@@ -36,6 +30,9 @@ def make_request(self, url, method='get', params=None, files=None, response_type
3630
return response.json()
3731
raise ValueError('Invalid response_type "{0}"'.format(response_type))
3832

33+
def __call__(self, *args, **kwargs):
34+
return self.make_request(*args, **kwargs)
35+
3936

4037
class TelegramApiInterface:
4138
def __init__(self, token, request_executor, api_url=API_URL, file_url=FILE_URL):
@@ -61,7 +58,8 @@ def convert_inline_results(results):
6158
def make_request(self, method_name, params=None, files=None, method='get', response_type='json'):
6259
request_url = self.api_url.format(self.token, method_name)
6360
try:
64-
response = self.request_executor.make_request(request_url, method, params, files, response_type)
61+
response = self.request_executor(request_url,
62+
method=method, params=params, files=files, response_type=response_type)
6563
return response
6664
except Exception as e:
6765
raise ApiException(repr(e), method_name)

telebot/listeners.py

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
# -*- coding: utf-8 -*-
2+
import re
3+
4+
from telebot import util
5+
6+
7+
class MessageHandler:
8+
9+
def __init__(self, handler, commands=None, regexp=None, func=None, content_types=None):
10+
self.handler = handler
11+
self.tests = []
12+
if content_types is not None:
13+
self.tests.append(lambda m: m.content_type in content_types)
14+
15+
if commands is not None:
16+
self.tests.append(lambda m: m.content_type == 'text' and util.extract_command(m.text) in commands)
17+
18+
if regexp is not None:
19+
self.tests.append(lambda m: m.content_type == 'text' and re.search(regexp, m.text))
20+
21+
if func is not None:
22+
self.tests.append(func)
23+
24+
def test_message(self, message):
25+
return all([test(message) for test in self.tests])
26+
27+
def __call__(self, update):
28+
if update.message is None:
29+
return
30+
31+
if self.test_message(update.message):
32+
self.handler(update.message)
33+
34+
35+
class NextStepHandler:
36+
37+
def __init__(self, handler, message):
38+
self.chat_id = message.chat.id
39+
self.handler = handler
40+
41+
def __call__(self, update):
42+
if update.message is None:
43+
return
44+
45+
if update.message.chat.id == self.chat_id:
46+
self.handler(update.message)
47+
48+
49+
class InlineHandler:
50+
51+
def __init__(self, handler, func):
52+
self.handler = handler
53+
self.func = func
54+
55+
def __call__(self, update):
56+
if update.inline_query is None:
57+
return
58+
59+
if self.func(update.inline_query):
60+
self.handler(update.inline_query)
61+
62+
63+
class ChosenInlineResultHandler:
64+
65+
def __init__(self, handler, func):
66+
self.handler = handler
67+
self.func = func
68+
69+
def __call__(self, update):
70+
if update.chosen_inline_result is None:
71+
return
72+
73+
if self.func(update.chosen_inline_result):
74+
self.handler(update.chosen_inline_result)
75+
76+
77+
class CallbackQueryHandler:
78+
79+
def __init__(self, handler, func):
80+
self.handler = handler
81+
self.func = func
82+
83+
def __call__(self, update):
84+
if update.callback_query is None:
85+
return
86+
87+
if self.func(update.callback_query):
88+
self.handler(update.callback_query)

0 commit comments

Comments
 (0)