Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using telethon with django: There is no current event loop in thread 'Thread-1'

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')
like image 424
SasaN AndeH Avatar asked Oct 27 '25 08:10

SasaN AndeH


1 Answers

My solution:

 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)
like image 51
Cù Phan Avatar answered Oct 28 '25 21:10

Cù Phan