GithubHelp home page GithubHelp logo

licht1stein / telethon-session-sqlalchemy Goto Github PK

View Code? Open in Web Editor NEW

This project forked from tulir/telethon-session-sqlalchemy

0.0 0.0 0.0 33 KB

SQLAlchemy backend for Telethon session storage

License: MIT License

Python 100.00%

telethon-session-sqlalchemy's Introduction

Telethon SQLAlchemy session

A Telethon session storage implementation backed by SQLAlchemy.

Installation

telethon-session-sqlalchemy @ PyPI

pip install telethon-session-sqlalchemy

Usage

This session implementation can store multiple Sessions in the same database, but to do this, each session instance needs to have access to the same models and database session.

To get started, you need to create an AlchemySessionContainer which will contain that shared data. The simplest way to use AlchemySessionContainer is to simply pass it the database URL:

from alchemysession import AlchemySessionContainer
container = AlchemySessionContainer('postgres://user:pass@localhost/telethon')

If you already have SQLAlchemy set up for your own project, you can also pass the engine separately:

my_sqlalchemy_engine = sqlalchemy.create_engine('...')
container = AlchemySessionContainer(engine=my_sqlalchemy_engine)

By default, the session container will manage table creation/schema updates/etc automatically. If you want to manage everything yourself, you can pass your SQLAlchemy Session and declarative_base instances and set manage_tables to False:

from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import orm
import sqlalchemy
...
session_factory = orm.sessionmaker(bind=my_sqlalchemy_engine)
session = session_factory()
my_base = declarative_base()
...
container = AlchemySessionContainer(
    session=session, table_base=my_base, manage_tables=False
)

You always need to provide either engine or session to the container. If you set manage_tables=False and provide a session, engine is not needed. In any other case, engine is always required.

After you have your AlchemySessionContainer instance created, you can create new sessions by calling new_session:

session = container.new_session('some session id')
client = TelegramClient(session)

where some session id is an unique identifier for the session.

telethon-session-sqlalchemy's People

Contributors

14mrh4x0r avatar tulir 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.