Skip to main content

DepyTG

The only Python3 Telegram bot library that does nothing.

Wait, what?

Well of course it doesn't do nothing at all. However, it does nothing compared to many other Telegram bot libraries, and that's a design goal.

Design goals

The main goal is to KISS — Keep It Simple, Stupid.

Other than being simple, DepyTG tries to:

  • Have a 1:1 correspondence with Telegram's official API specs. The only documentation you need is Telegram's.
  • Be compatible with any HTTP library you may want to use — Requests, Flask, JSON+Urllib, anything
  • Make sure 99.9999% of its objects are JSON-serializable
  • Provide a simple (but totally optional) API to do the network stuff
  • Heavily integrate with IDEs that support code insights by type-hinting everything that can be type-hinted
  • Not try to reinvent the wheel. Telegram's API is quite simple, we're not going to implement simplified "send_message" methods.

Big note

This is a work in progress! I wrote this from scratch to write my own Telegram bots. I haven't tested it very much. I'll test it as I work on them. I'll write some tests and add CI soon.

Quick intro

Creating an object
  • Manually
>>> types.Document(
       file_id='doc_id',
       file_name='ciao.pdf',
       thumb=PhotoSize(
                 file_id='thumb_id',
                 width=100, 
                 height=50
             )
    )
 Document({'file_id': 'doc_id', 'thumb': PhotoSize({'file_id': 'thumb_id', 'width': 100, 'height': 50}), 'file_name': 'ciao.pdf'})
  • From dict/JSON
>>> types.Document.from_json({'file_id': 'doc_id',
'file_name': 'ciao.pdf', 'thumb': {'file_id': 'thumb_id', 'height': 50, 'width': 100}})
Document({'file_id': 'doc_id', 'thumb': PhotoSize({'file_id': 'thumb_id', 'width': 100, 'height': 50}), 'file_name': 'ciao.pdf'})

# Notice how the type of "thumb" (PhotoSize) is automatically detected.

Calling methods

Methods are regular Python objects. "Call" them once to generate the parameters' dictionary, then twice (passing the API token) to actually send the request.

  • With the built-in API
#                     ↓ Pass fields here              ↓ Pass token here 
>>> methods.setWebhook("https://my.super.webhook.com")("my_bot_token")
True
>>> methods.getWebhookInfo()("my_bot_token")
WebhookInfo({'url': 'https://my.super.webhook.com', 'has_custom_certificate': False, 'pending_update_count': 0})
  • With an external library
#   ↓ Store to variable        ↓ Only pass fields
>>> method = methods.setWebhook("https://my.super.webhook.com")
>>> r = requests.post("https://api.telegram.org/botmy_bot_token/setWebhook", json=method)
>>> method.read_result(r.json())
True

>>> r = requests.get("https://api.telegram.org/botmy_bot_token/getWebhookInfo")
#   ↓ "read_result" is specific for each method
>>> methods.getWebhookInfo.read_result(r.json())
WebhookInfo({'url': 'https://my.super.webhook.com', 'has_custom_certificate': False, 'pending_update_count': 0})
Note:

Methods that take InputFile objects are a bit special. First of all, any field that takes InputFile is made optional, even if Telegram's API references says the opposite.

InputFile is the only object that is not JSON-serializable and as such, it needs special handling. If you use a custom HTTP library, you will need to upload the files yourself as described in Telegram's documentation.

The built-in requests API will handle InputFile objects automatically and send the fields as multipart/form-data. If requests-toolbelt is installed it will be used to stream the file.

Possible questions

Why the hell did you define every possible object in the API?

Because one of my goals was to have code completion.

All Telegram API objects and methods are children of TelegramObjectBase, which is a subclass of dict. This means everything (except for InputFile) is JSON-serializable and can be used outside of this library.

dict has been extended to check field types and to access them with the standard dot notation, so that IDEs like PyCharm can warn you if you do something wrong.

The reason they were "rewritten" is to allow for type checking.

Metadata

Release files for DepyTG 4.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for DepyTG 4.0.0
File Size Uploaded
DepyTG-4.0.0.tar.gz 36.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for DepyTG 4.0.0
File Interpreter ABI Platform
DepyTG-4.0.0-py2.py3-none-any.whl Python 2, Python 3 none any Details

Total release size: 76.2 kB

Release files / DepyTG-4.0.0.tar.gz

Download URL DepyTG-4.0.0.tar.gz
Size 36.9 kB
Tags Source
SHA-256 checksum
How to use checksums
f126f7819752b14ef628d0ccf458e962e3b3c682868c300e34ad1f1559f74569
BLAKE2b-256 checksum
How to use checksums
07f06d9a686bff2ea04193c9abf68d620e7404445aa4f5e9e84978605c70e299
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.2.0 requests-toolbelt/0.8.0 tqdm/4.25.0 CPython/3.7.0

Release files / DepyTG-4.0.0-py2.py3-none-any.whl

Download URL DepyTG-4.0.0-py2.py3-none-any.whl
Size 39.3 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
6c2489c3af28f720ad9dd6dfa0991329fa31ae79b309735b8f6d3e0212f2e1e8
BLAKE2b-256 checksum
How to use checksums
351872d6fedc0d74f60e2ba199a8ceb6caf8c9941845e2a06eb2a6715fd66aae
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.2.0 requests-toolbelt/0.8.0 tqdm/4.25.0 CPython/3.7.0

Release history Release notifications | RSS feed

This release

4.0.0 This release

2 release files

3.6.5

2 release files

3.6.3

2 release files

3.6.2

2 release files

3.6.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page