4

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') 
2
  • stackoverflow.com/q/44667242 may help. Commented May 24, 2020 at 8:37
  • could you find the solution? Commented Oct 1, 2020 at 16:38

2 Answers 2

9

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) 
Sign up to request clarification or add additional context in comments.

Comments

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 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.