title: Telethon - How do I setup a simple web app (using Quart for example)?
body: <p>I'm doing a small project written in PHP (Laravel framework) and a part of project has to do telegram messaging. I couldn't find any MTProto library written in PHP that is well supported &amp; well documented. I know there is MadelineProto but it's missing many features and it ain't big and loved like how Telethon is.</p>
  
  <p>So I thought maybe I could just setup a simple python script (even though I have zero experience with Python) where it provides a simple API that I can reach from the PHP side and call those methods and use them. (Let me know if there are any better solutions)</p>
  
  <p>My first thought was to use Flask with Telethon but as you may know Telethon is a asyncio library and it doesn't integrate with Flask easily. And I found about this helpful <a href="https://github.com/LonamiWebs/Telethon/tree/master/telethon_examples#quart_loginpy" rel="nofollow noreferrer">link</a> which explains some stuff and talks about Quart and provides a (supposedly) working example.</p>
  
  <p>I tried to follow it like so (Python 3.8) :</p>
  
  <pre class="lang-py prettyprint-override"><code>from telethon import TelegramClient, connection
  import hypercorn.asyncio
  from quart import Quart
  
  api_id = x
  api_hash = x
  
  client = TelegramClient('telegram', api_id, api_hash)
  
  app = Quart(__name__)
  
  @app.before_serving
  async def startup():
      await client.connect()
  
  
  @app.after_serving
  async def cleanup():
      await client.disconnect()
  
  @app.route('/')
  async def hello_world():
      await client.send_message('me', 'Hello World')
      return 'Message Sent!'
  
  async def main():
      await hypercorn.asyncio.serve(app, hypercorn.Config())
  
  if __name__ == '__main__':
      client.loop.run_until_complete(main())
  </code></pre>
  
  <p>I know in the quart <a href="https://github.com/LonamiWebs/Telethon/blob/master/telethon_examples/quart_login.py" rel="nofollow noreferrer">example</a> they manually do the login but I don't need that. In my case I'm only working with 1 global client and I already logged it in using Telethon built-in interactive login (by running <code>python run.app</code> for the first time where it creates the session file)
  Anyway the hello world message does gets sent but the problem is I'm getting all kind of different errors in the console output.</p>
  
  <p>Like if I try to stop the app using (CTRL + C) after running <code>python run.py</code> I get this error:</p>
  
  <p><code>RuntimeError: no running event loop
  Task was destroyed but it is pending</code></p>
  
  <p>Or if I try to run the code using <code>hypercorn run:app</code> I get these 2 errors immediately and the app won't even start:</p>
  
  <p><code>got Future &lt;Future pending&gt; attached to a different loop
  RuntimeWarning: coroutine 'open_connection' was never awaited</code></p>
  
  <p>(I removed all the traceback for the sake of keeping the question clean)</p>
  
  <p>Here are my questions:</p>
  
  <ol>
  <li>I don't know if I'm doing the <a href="https://github.com/LonamiWebs/Telethon/blob/master/telethon_examples/quart_login.py" rel="nofollow noreferrer">example</a> wrong maybe I'm missing something?</li>
  <li>Should I use a different library other than Quart? or it wouldn't make any difference?</li>
  <li>Maybe I approached the problem the wrong way and I should use a different solution to have Telethon work with PHP ?</li>
  </ol>