2

I want to use Telethon from the django. But when I am running it, I am getting following error:

RuntimeError: There is no current event loop in thread 'Thread-1'.

my code views.py:

from django.shortcuts import render,HttpResponse
from telethon.sync import TelegramClient, events

async def join(client):
    ch = '@andeh_ir'
    try:
        await client(JoinChannelRequest(ch))
        print('[+] Joined The Channel')
    except:
        print('[-] skiped')

def addChannel(request):
    api_id   =   XXXXXX
    api_hash = 'xxxxxxxxxxxxxxxxxxxxx'
    client = TelegramClient('+254716550762', api_id, api_hash )
    with client:
        client.loop.run_until_complete(join(client))
    return HttpResponse('addChannel')
SasaN AndeH
  • 23
  • 1
  • 5

2 Answers2

6

My solution #import asyncio

 import asyncio
 api_id = xxxx
 api_hash = 'b772662cf8a918bfbc39ee2aee36f6cc'
 loop = asyncio.new_event_loop()
 asyncio.set_event_loop(loop)
 client = TelegramClient(phone, api_id, api_hash, loop=loop)
Cù Phan
  • 61
  • 1
  • 2
0

You can use django-telethon and it's support multiple sessions.

You can use the API endpoints/admin panel/interactive for signing bot and user sessions.

  1. run the following command to start the server:

    python manage.py runserver
    
  2. run the following command to start telegram client:

    python manage.py runtelegram
    
Ali ZahediGol
  • 876
  • 2
  • 10
  • 20