Skip to main content

Asyncpg powered session for pgsql databases

Project description

Async session for telethon

Installation

pip install telethon_asyncpg
# Or with poetry
poetry add telethon_asyncpg

Usage

import ssl  # optional

from telethon import events, TelegramClient

from telethon_asyncpg import AsyncpgSession, install
install()

URI = ???  # URI-string
# dialect+driver://username:password@host:port/database

pgconf = dict(dsn=URI, min_size=5, max_size=5)
# to overcome problem with TLS connection to db pass
# ssl=ssl.SSLContext(protocol=ssl.PROTOCOL_TLS) to pgconf
session = AsyncpgSession(pgconf, session_id_factory=???)
# session_id_factory is any callable with "() -> str" signature
# default factory is uuid4 str generator. why factory? (it's not really factory ik)
bot = TelegramClient(session=session, api_id=???, api_hash=???)

@bot.on(events.NewMessage())
async def message_handler(message):
    await message.reply("Hi!")

async def start():
    await bot.start()
    print(await bot.get_entity("martin_winks"))
    await bot.run_until_disconnected()

if __name__ == '__main__':
    import asyncio
    asyncio.get_event_loop().run_until_complete(start())
  • AsyncpgSession can also use shared pool by AsyncpgSession.with_pool initializer-method

my_pool = asyncpg.create_pool(...)
session = AsyncpgSession.with_pool(my_pool, lambda: "session-id", True)

Check out the examples/ folder for more realistic examples.

Contribution

Currently we have only asyncpg session available, if you want to contribute with your wrapper - welcome. Take AsyncpgSession as an example.

For contributors

Patched TelegramClient <-> Session

  • TelegramClient and Session object share settings {session.meth: (args_seq, kwargs_mapping)} dictionary. By protocol TelegramClient must add callable with args and kwargs. By protocol session must call this functions at start as it wants (e.g. pass more arguments such as connection object to session.method)

  • TelegramClient may call session.start several times per one session instance and session should control its start itself and if it’s already started it shouldn’t start again

  • Session.save method is guaranteed to be called as in usual telethon

Hacking

# install poetry dependency manager
# Fork/Fork+Clone && cd {{cloned}}
poetry install
# happy hacking!

References

Telethon: here asyncpg pg-driver: asyncpg

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

telethon_asyncpg-0.2.0.tar.gz (51.5 kB view details)

Uploaded Source

Built Distribution

telethon_asyncpg-0.2.0-py3-none-any.whl (56.4 kB view details)

Uploaded Python 3

File details

Details for the file telethon_asyncpg-0.2.0.tar.gz.

File metadata

  • Download URL: telethon_asyncpg-0.2.0.tar.gz
  • Upload date:
  • Size: 51.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.0.5 CPython/3.8.0 Linux/4.15.0-101-generic

File hashes

Hashes for telethon_asyncpg-0.2.0.tar.gz
Algorithm Hash digest
SHA256 44e7804ee9eaee111fce2f93196213827299b0def4c55f57473e0b9453e5ce77
MD5 66067ced0ece6c5fb63e58567a778066
BLAKE2b-256 9bd1bf1db685eb1e919c6b191ffaf083da62f37d99313b9cf6f16b6d7398d1e3

See more details on using hashes here.

File details

Details for the file telethon_asyncpg-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: telethon_asyncpg-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 56.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.0.5 CPython/3.8.0 Linux/4.15.0-101-generic

File hashes

Hashes for telethon_asyncpg-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 678d43efcb9b2cc4725b19720f3b1e06c50bb2f2472bfae9a5237ac4eab204b8
MD5 f62f901db6e3f54ce4a2359bae5057d5
BLAKE2b-256 308e3c9af1a43c8ffb8e391629d81557a1a561a92e958360ec6cff7c7595ebcf

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page