mirror of
https://github.com/eternnoir/pyTelegramBotAPI.git
synced 2023-08-10 21:12:57 +03:00
commit
2623fa362c
34
README.md
34
README.md
@ -12,7 +12,7 @@
|
|||||||
|
|
||||||
## Contents
|
## Contents
|
||||||
|
|
||||||
* [Getting started.](#getting-started)
|
* [Getting started](#getting-started)
|
||||||
* [Writing your first bot](#writing-your-first-bot)
|
* [Writing your first bot](#writing-your-first-bot)
|
||||||
* [Prerequisites](#prerequisites)
|
* [Prerequisites](#prerequisites)
|
||||||
* [A simple echo bot](#a-simple-echo-bot)
|
* [A simple echo bot](#a-simple-echo-bot)
|
||||||
@ -49,6 +49,7 @@
|
|||||||
* [Using web hooks](#using-web-hooks)
|
* [Using web hooks](#using-web-hooks)
|
||||||
* [Logging](#logging)
|
* [Logging](#logging)
|
||||||
* [Proxy](#proxy)
|
* [Proxy](#proxy)
|
||||||
|
* [Testing](#testing)
|
||||||
* [API conformance](#api-conformance)
|
* [API conformance](#api-conformance)
|
||||||
* [F.A.Q.](#faq)
|
* [F.A.Q.](#faq)
|
||||||
* [How can I distinguish a User and a GroupChat in message.chat?](#how-can-i-distinguish-a-user-and-a-groupchat-in-messagechat)
|
* [How can I distinguish a User and a GroupChat in message.chat?](#how-can-i-distinguish-a-user-and-a-groupchat-in-messagechat)
|
||||||
@ -57,7 +58,7 @@
|
|||||||
* [More examples](#more-examples)
|
* [More examples](#more-examples)
|
||||||
* [Bots using this API](#bots-using-this-api)
|
* [Bots using this API](#bots-using-this-api)
|
||||||
|
|
||||||
## Getting started.
|
## Getting started
|
||||||
|
|
||||||
This API is tested with Python 3.6-3.9 and Pypy 3.
|
This API is tested with Python 3.6-3.9 and Pypy 3.
|
||||||
There are two ways to install the library:
|
There are two ways to install the library:
|
||||||
@ -622,7 +623,6 @@ When using webhooks telegram sends one Update per call, for processing it you sh
|
|||||||
There are some examples using webhooks in the [examples/webhook_examples](examples/webhook_examples) directory.
|
There are some examples using webhooks in the [examples/webhook_examples](examples/webhook_examples) directory.
|
||||||
|
|
||||||
### Logging
|
### Logging
|
||||||
|
|
||||||
You can use the Telebot module logger to log debug info about Telebot. Use `telebot.logger` to get the logger of the TeleBot module.
|
You can use the Telebot module logger to log debug info about Telebot. Use `telebot.logger` to get the logger of the TeleBot module.
|
||||||
It is possible to add custom logging Handlers to the logger. Refer to the [Python logging module page](https://docs.python.org/2/library/logging.html) for more info.
|
It is possible to add custom logging Handlers to the logger. Refer to the [Python logging module page](https://docs.python.org/2/library/logging.html) for more info.
|
||||||
|
|
||||||
@ -634,7 +634,6 @@ telebot.logger.setLevel(logging.DEBUG) # Outputs debug messages to console.
|
|||||||
```
|
```
|
||||||
|
|
||||||
### Proxy
|
### Proxy
|
||||||
|
|
||||||
You can use proxy for request. `apihelper.proxy` object will use by call `requests` proxies argument.
|
You can use proxy for request. `apihelper.proxy` object will use by call `requests` proxies argument.
|
||||||
|
|
||||||
```python
|
```python
|
||||||
@ -649,6 +648,33 @@ If you want to use socket5 proxy you need install dependency `pip install reques
|
|||||||
apihelper.proxy = {'https':'socks5://userproxy:password@proxy_address:port'}
|
apihelper.proxy = {'https':'socks5://userproxy:password@proxy_address:port'}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### Testing
|
||||||
|
You can disable or change the interaction with real Telegram server by using
|
||||||
|
```python
|
||||||
|
apihelper.CUSTOM_REQUEST_SENDER = your_handler
|
||||||
|
```
|
||||||
|
parameter. You can pass there your own function that will be called instead of _requests.request_.
|
||||||
|
|
||||||
|
For example:
|
||||||
|
```python
|
||||||
|
def custom_sender(method, url, **kwargs):
|
||||||
|
print("custom_sender. method: {}, url: {}, params: {}".format(method, url, kwargs.get("params")))
|
||||||
|
result = util.CustomRequestResponse('{"ok":true,"result":{"message_id": 1, "date": 1, "chat": {"id": 1, "type": "private"}}}')
|
||||||
|
return result
|
||||||
|
```
|
||||||
|
|
||||||
|
Then you can use API and proceed requests in your handler code.
|
||||||
|
```python
|
||||||
|
apihelper.CUSTOM_REQUEST_SENDER = custom_sender
|
||||||
|
tb = TeleBot("test")
|
||||||
|
res = tb.send_message(123, "Test")
|
||||||
|
```
|
||||||
|
|
||||||
|
Result will be:
|
||||||
|
|
||||||
|
`custom_sender. method: post, url: https://api.telegram.org/botololo/sendMessage, params: {'chat_id': '123', 'text': 'Test'}`
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
## API conformance
|
## API conformance
|
||||||
|
|
||||||
|
@ -40,10 +40,12 @@ RETRY_TIMEOUT = 2
|
|||||||
MAX_RETRIES = 15
|
MAX_RETRIES = 15
|
||||||
|
|
||||||
CUSTOM_SERIALIZER = None
|
CUSTOM_SERIALIZER = None
|
||||||
|
CUSTOM_REQUEST_SENDER = None
|
||||||
|
|
||||||
ENABLE_MIDDLEWARE = False
|
ENABLE_MIDDLEWARE = False
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def _get_req_session(reset=False):
|
def _get_req_session(reset=False):
|
||||||
if SESSION_TIME_TO_LIVE:
|
if SESSION_TIME_TO_LIVE:
|
||||||
# If session TTL is set - check time passed
|
# If session TTL is set - check time passed
|
||||||
@ -136,9 +138,14 @@ def _make_request(token, method_name, method='get', params=None, files=None):
|
|||||||
method, request_url, params=params, files=files,
|
method, request_url, params=params, files=files,
|
||||||
timeout=(connect_timeout, read_timeout), proxies=proxy)
|
timeout=(connect_timeout, read_timeout), proxies=proxy)
|
||||||
else:
|
else:
|
||||||
result = _get_req_session().request(
|
if CUSTOM_REQUEST_SENDER:
|
||||||
method, request_url, params=params, files=files,
|
result = CUSTOM_REQUEST_SENDER(
|
||||||
timeout=(connect_timeout, read_timeout), proxies=proxy)
|
method, request_url, params=params, files=files,
|
||||||
|
timeout=(connect_timeout, read_timeout), proxies=proxy)
|
||||||
|
else:
|
||||||
|
result = _get_req_session().request(
|
||||||
|
method, request_url, params=params, files=files,
|
||||||
|
timeout=(connect_timeout, read_timeout), proxies=proxy)
|
||||||
|
|
||||||
logger.debug("The server returned: '{0}'".format(result.text.encode('utf8')))
|
logger.debug("The server returned: '{0}'".format(result.text.encode('utf8')))
|
||||||
|
|
||||||
|
@ -12,6 +12,11 @@ import logging
|
|||||||
|
|
||||||
from telebot import types
|
from telebot import types
|
||||||
|
|
||||||
|
try:
|
||||||
|
import ujson as json
|
||||||
|
except ImportError:
|
||||||
|
import json
|
||||||
|
|
||||||
try:
|
try:
|
||||||
# noinspection PyPackageRequirements
|
# noinspection PyPackageRequirements
|
||||||
from PIL import Image
|
from PIL import Image
|
||||||
@ -165,6 +170,16 @@ class AsyncTask:
|
|||||||
return self.result
|
return self.result
|
||||||
|
|
||||||
|
|
||||||
|
class CustomRequestResponse():
|
||||||
|
def __init__(self, json_text, status_code = 200, reason = ""):
|
||||||
|
self.status_code = status_code
|
||||||
|
self.text = json_text
|
||||||
|
self.reason = reason
|
||||||
|
|
||||||
|
def json(self):
|
||||||
|
return json.loads(self.text)
|
||||||
|
|
||||||
|
|
||||||
def async_dec():
|
def async_dec():
|
||||||
def decorator(fn):
|
def decorator(fn):
|
||||||
def wrapper(*args, **kwargs):
|
def wrapper(*args, **kwargs):
|
||||||
|
Loading…
Reference in New Issue
Block a user