Interface#
This module implements various interfaces for TelegramMessenger
that can be used to interact with the Telegram API.
- extract_telegram_request_and_id(update, messenger=None)[source]#
Utility function that extracts parameters from a telegram update. Changes the messenger state, setting the last update id.
Returned message has the following fields:
- update_id – this field stores update.update_id,
- update – this field stores the first non-empty field of update,
- update_type – this field stores the name of the first non-empty field of update,
- text – this field stores update.message.text,
- callback_query – this field stores update.callback_query.data.
Also return context id which is chat, from_user or user of the update.
- Parameters:
update (
Update
) – Update to process.messenger (
Optional
[TelegramMessenger
]) – Messenger instance. If passed updates last_update_id. Defaults to None.
- Return type:
Tuple
[TelegramMessage
,int
]
- class PollingTelegramInterface(token, interval=2, allowed_updates=None, timeout=20, long_polling_timeout=20, messenger=None)[source]#
Bases:
MessengerInterface
Telegram interface that retrieves updates by polling. Multi-threaded polling is currently not supported.
- Parameters:
token (
str
) – Bot tokenmessenger (
Optional
[TelegramMessenger
]) –TelegramMessenger
instance. If not None will be used instead of creating messenger from token. Token value does not matter in that case. Defaults to None.interval (
int
) – Polling interval. See link. Defaults to 2.allowed_updates (
Optional
[List
[str
]]) – Processed updates. See link. Defaults to None.timeout (
int
) – General timeout. See link. Defaults to 20.long_polling_timeout (
int
) – Polling timeout. See link. Defaults to 20.
- async connect(callback, loop=None, *args, **kwargs)[source]#
Method invoked when message interface is instantiated and connection is established. May be used for sending an introduction message or displaying general bot information.
- Parameters:
pipeline_runner – A function that should process user request and return context; usually it’s a
_run_pipeline()
function.
- class CallbackTelegramInterface(token, app=None, host='localhost', port=8443, debug=None, load_dotenv=True, endpoint='/telegram-webhook', full_uri=None, messenger=None, **wsgi_options)[source]#
Bases:
CallbackMessengerInterface
Asynchronous Telegram interface that retrieves updates via webhook. Any Flask server can be passed to set up a webhook on a separate endpoint.
- Parameters:
token (
str
) – Bot tokenmessenger (
Optional
[TelegramMessenger
]) –TelegramMessenger
instance. If not None will be used instead of creating messenger from token. Token value does not matter in that case. Defaults to None.app (
Optional
[Flask
]) – Flask instance. Defaults to Flask(__name__).endpoint (
str
) – Webhook endpoint. Should be prefixed with “/”. Defaults to “/telegram-webhook”.host (
str
) – Host IP. Defaults to “localhost”.port (
int
) – Port of the app. Defaults to 8443.debug (
Optional
[bool
]) – Run the Flask app in debug mode.load_dotenv (
bool
) – Whether or not the .env file in the project folder should be used to set environment variables.full_uri (
Optional
[str
]) – Full public IP of your webhook that is accessible by https. Defaults to “https://{host}:{port}{endpoint}”.wsgi_options – Keyword arguments to forward to Flask.run method. Use these to set ssl_context and other WSGI options.
- async connect(callback)[source]#
Method invoked when message interface is instantiated and connection is established. May be used for sending an introduction message or displaying general bot information.
- Parameters:
pipeline_runner – A function that should process user request and return context; usually it’s a
_run_pipeline()
function.