This is a python library for store your telethon sessions in mongodb database. The main feature is that you can store several telethon sessions in one Mongodb database.
Tip
This is achieved due to the fact that each session is a single document that has a unique composite key (phone number, API ID, API Hash)
pip install git+https://github.com/devtolmachev/telethon-mongo-session
import pymongo
import telethon as th
from telethon_multimongo_session import MongoSession
session = MongoSession(
api_id=API_ID,
api_hash=API_HASH,
phone=PHONE_NUMBER,
database=MONGODB_TEST_DBNAME,
coll=MONGODB_TEST_COLL,
host=MONGODB_HOST,
port=MONGODB_PORT,
)
client = th.TelegramClient(session, api_id=API_ID, api_hash=API_HASH)
# Set dc for test servers
dc_id = int(PHONE_NUMBER[5])
# Get dc ip
ip = TELEGRAM_DATA_CENTERS_IP[dc_id]
# Set dc in session
client.session.set_dc(dc_id, ip, 80)
# Connect to telegram and write some data to session
await telethon.start(phone=PHONE_NUMBER, code_callback=lambda: CODE)
me = await telethon.get_me()
assert me
Tests stored in tests/
directory, you may also run all tests by typing in terminal make tests