0

I want to watch updates on telegram messages in an django application and interact with django orm.

I found telethon library, it works with user api which is what I want.

Below code simply works on its own.

from telethon import TelegramClient
from telethon import events

api_id = 231232131
api_hash = '32131232312312312edwq'
client = TelegramClient('anon', api_id, api_hash)


@client.on(events.NewMessage)
async def my_event_handler(event):
    if 'hello' in event.raw_text:
        await event.reply('hi!')


client.start()

But telethon requires phone message verification and it needs to work in a seperate thread.

I couldn't find a way to put this code in a django application. And when django starts, I dont know how to bypass phone verification.

It should always work in an seperate loop and interact with django orm. Which is very confusing for me.

durdenk
  • 1,426
  • 1
  • 13
  • 32
  • See https://docs.telethon.dev/en/latest/quick-references/faq.html#can-i-use-flask-with-the-library, which is a similar problem. [How to combine python asyncio with threads?](https://stackoverflow.com/q/28492103/4759433) may also help you. – Lonami Sep 02 '21 at 10:43

1 Answers1

0

It's not the answer I wanted initially.

But, I think this is better approach.

Instead of trying to put all of this in django application. It's better to run it seperately and let django application communicate this with rest framework.

durdenk
  • 1,426
  • 1
  • 13
  • 32