Giter Site home page Giter Site logo

telethon_asyncpg's Introduction

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

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

telethon_asyncpg's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.