How to use telethon in Flask

See original GitHub issue

Here is my code in flask and i get an error after calling auth_code asking me to call send_request first. `

import json
from flask import Flask
from flask import Response
from telethon import TelegramClient
from telethon.tl.functions.contacts import GetContactsRequest
from telethon.tl.types import UserStatusOffline

api_id = 1000000
api_hash = 'XXXXXXXXXXXXXXXXXXXXXXXXXXX'

app = Flask(__name__)


@app.route('/auth/<phone>', methods=['POST'])
def auth_phone(phone):
    client = TelegramClient("my_name", api_id, api_hash)
    client.connect()

    response = {'code_sent': False}

    if not client.is_user_authorized():
        client.send_code_request("+{}".format(phone))
        response['code_sent'] = True

    return Response(json.dumps(response), 200, mimetype="application/json")


@app.route('/auth/<phone>/<code>', methods=['POST'])
def auth_code(phone, code):
    client = TelegramClient("my_name", api_id, api_hash)
    client.connect()

    if not client.is_user_authorized():
        client.send_code_request("+{}".format(phone))
        client.sign_in(phone, code)

    response = {'logged_in': False}

    if client.is_user_authorized():
        response['logged_in'] = True

    return Response(json.dumps(response), 200, mimetype="application/json")`

Please make sure to call send_code_request first.

Thanks in anticipation.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
ForsakenHarmonycommented, Feb 3, 2020

Ok boomer

0reactions
Lonamicommented, Feb 3, 2020

@clintnetwork after a thousand issues in a large project, there is a point where you have to start being more strict. This work is something I do for free, on my free time, and because I want to. I have no obligation on doing other people’s job. This is my repository, and these are my rules. You do not need to use this project or lurk here if you don’t like it, or if you don’t like me. There is no need to go off-topic in the comments, and there is much less of a reason to be hostile towards other people (including myself).

I have reported your comment to GitHub, to let them decide on this one.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Python Flask with Telethon - Stack Overflow
Flask is threaded. Telethon uses asyncio. Using threads and asyncio is an unnecessary headache, even if you know what you're doing. I have ......
Read more >
FAQ — Telethon 1.26.0 documentation
FAQ¶. Let's start the quick references section with some useful tips to keep in mind, with the hope that you will understand why...
Read more >
Telethon Documentation - Read the Docs
To run the code, use python3 hello.py from the terminal. Important: Don't call your script telethon.py! Python will try to import the client ......
Read more >
How do you use Telethon?
How do you use Telethon? · Follow this link and login with your phone number. · Click under API Development tools. · A...
Read more >
Creating a Telegram bot in python with Telethon library (Quiz ...
... and follow the instruction In this video we will see how to build a chatbot for telegram using python and the Telethon...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found