Mbonea commited on
Commit
ad95bb0
·
1 Parent(s): 6f39684

context sessiion

Browse files
Files changed (2) hide show
  1. App/__init__.py +23 -25
  2. App/app.py +12 -4
App/__init__.py CHANGED
@@ -1,10 +1,10 @@
1
  import uuid, contextvars
2
  from telethon import TelegramClient
3
- from telethon.sync import TelegramClient as Tlsync
4
- from telethon.sessions import StringSession
 
5
 
6
 
7
- import os
8
 
9
 
10
  def genUUID():
@@ -13,28 +13,26 @@ def genUUID():
13
  return short_uuid
14
 
15
 
16
- SESSION_STRING = ""
17
- with Tlsync(
18
- f"./App/{genUUID()}.session",
19
- api_id=870972,
20
- api_hash="ce2efaca02dfcd110941be6025e9ac0d",
21
- ) as client:
22
- client.start(bot_token="6183919505:AAEhHFt4mI18bQeAf2Lj7AePXFRPVLrOFM8")
23
- ... # use the client
24
-
25
- # Save the string session as a string; you should decide how
26
- # you want to save this information (over a socket, remote
27
- # database, print it and then paste the string in the code,
28
- # etc.); the advantage is that you don't need to save it
29
- # on the current disk as a separate file, and can be reused
30
- # anywhere else once you log in.
31
- SESSION_STRING = client.session.save()
32
-
33
- bot = TelegramClient(
34
- StringSession(SESSION_STRING),
35
- api_id=870972,
36
- api_hash="ce2efaca02dfcd110941be6025e9ac0d",
37
- )
38
 
39
 
40
 
 
1
  import uuid, contextvars
2
  from telethon import TelegramClient
3
+ import contextvars
4
+
5
+
6
 
7
 
 
8
 
9
 
10
  def genUUID():
 
13
  return short_uuid
14
 
15
 
16
+ SESSION=contextvars.ContextVar(None)
17
+ async def create_memsession():
18
+ async with TelegramClient(
19
+ f"./App/{genUUID()}.session",
20
+ api_id=870972,
21
+ api_hash="ce2efaca02dfcd110941be6025e9ac0d",
22
+ ) as client:
23
+ await client.start(bot_token="6183919505:AAEhHFt4mI18bQeAf2Lj7AePXFRPVLrOFM8")
24
+ ... # use the client
25
+
26
+ # Save the string session as a string; you should decide how
27
+ # you want to save this information (over a socket, remote
28
+ # database, print it and then paste the string in the code,
29
+ # etc.); the advantage is that you don't need to save it
30
+ # on the current disk as a separate file, and can be reused
31
+ # anywhere else once you log in.
32
+ temp= client.session.save()
33
+ SESSION.set(temp)
34
+
35
+ bot:TelegramClient = None
 
 
36
 
37
 
38
 
App/app.py CHANGED
@@ -1,9 +1,9 @@
1
  from fastapi import FastAPI, Request
2
  from fastapi.responses import JSONResponse
3
  from fastapi.middleware.gzip import GZipMiddleware
4
- from App import bot
5
-
6
-
7
  from .Users.UserRoutes import user_router
8
  from .modelInit import models, database
9
  from .Transcription.TranscriptionRoutes import transcription_router
@@ -47,7 +47,15 @@ def authjwt_exception_handler(request: Request, exc: AuthJWTException):
47
 
48
  @app.on_event("startup")
49
  async def startup_event():
50
- await bot.start(bot_token="6183919505:AAEhHFt4mI18bQeAf2Lj7AePXFRPVLrOFM8")
 
 
 
 
 
 
 
 
51
  # await upload_bot.start()
52
  # await models.create_all()
53
  # models.metadata.create_all()
 
1
  from fastapi import FastAPI, Request
2
  from fastapi.responses import JSONResponse
3
  from fastapi.middleware.gzip import GZipMiddleware
4
+ from App import bot,create_memsession,SESSION
5
+ from telethon import TelegramClient
6
+ from telethon.sessions import StringSession
7
  from .Users.UserRoutes import user_router
8
  from .modelInit import models, database
9
  from .Transcription.TranscriptionRoutes import transcription_router
 
47
 
48
  @app.on_event("startup")
49
  async def startup_event():
50
+ if not SESSION.get():
51
+ await create_memsession()
52
+ bot = TelegramClient(
53
+ StringSession(SESSION.get()),
54
+ api_id=870972,
55
+ api_hash="ce2efaca02dfcd110941be6025e9ac0d",
56
+ )
57
+
58
+ # await bot.start(bot_token="6183919505:AAEhHFt4mI18bQeAf2Lj7AePXFRPVLrOFM8")
59
  # await upload_bot.start()
60
  # await models.create_all()
61
  # models.metadata.create_all()