Skip to content

Python

Note: This is just a reference to learn how /AuthKit works.

In this document, I will guide you to create a project in Python using /AuthKit API to be able to confirm whether the user entered the correct OTP code sent.

Since we are working with APIs in Python, we will need to use a module called requests, and to protect AVC AuthKey, we will need to use the .env file and import it using the dotenv module. To install, go to Terminal and run the commands:

python -m pip install requests
python -m pip install python-dotenv

(Skip if already installed).


First, you need to create a .env file to protect your AVC AuthKey code:

AVC_AUTHKEY=<your-authkey>

Once done, we will start with the main.py file.

We will import the necessary modules to use at the beginning of the file:

from dotenv import load_dotenv
import os # Used to use variables in the `.env` file
import requests

Next we need to have code to launch dotenv to be able to get data from the .env file.

load_dotenv()

Since /AuthKit only supports two RESTful methods, GET and POST, the program will ask the user to choose the GET or POST method. If the user enters a value other than GET or POST, the program will report an error and ask for re-entry:

while True:
    method = input("RESTful Method (GET, POST): ")
    if method in ["GET", "POST"]:
        break
    else:
        print("Unsupported")

We will assign the AVC AuthKey code value in the .env file to the authkey variable:

authkey = os.getenv("AVC_AUTHKEY")

The program will ask the user for ChatID if the user chooses the POST method and sets 2 params avc_authkey and chat_id. If the user chooses the GET method, it will only take 1 param avc_authkey:

if method == "POST":
    chatid = input("ChatID: ")
    params = {"authkey": authkey, "chatid": chatid}
else:
    params = {"authkey": authkey}

The program will send an API request to the /AuthKit address with the method as method and the param as params:

response = requests.request(method, "https://avc.vercel.app/api/telegram/otpVerification", params=params)

Next, the program will check if the response is 200, it will parse the JSON response data and check if the method is POST, it will get the OTP code data from the codeVerification element in the JSON response code, if codeVerification does not exist, it will report an error. If it does, it will ask the user for the OTP code sent back. If it is correct, it will report success and close the program, otherwise it will report an error and ask for re-entry. Back to the problem of GET and POST. If it is GET, the program will parse the JSON response into variables and display it. Otherwise, if the response code is not 200, it will report an error with the error information:

if response.status_code == 200:
    data = response.json()

    if method == "POST":
        verification_code = data.get("verificationCode")
        if verification_code is None:
            print("Error: 'verificationCode' not found in the response.")
        else:
            while True:
                usr_verifycode = int(input("Verification Code: "))

                if usr_verifycode == verification_code:
                    print("Verification Code correct")
                    break
                else:
                    print("Verification Code incorrect. Try again")
    else:
        for key, value in data.items():
            if isinstance(value, dict):
                var_name = key
                vars_list = ", ".join(value.keys())
                print(f"{var_name} = [{vars_list}]")
                for subkey, subvalue in value.items():
                    print(f"{subkey} = {subvalue}")
            else:
                print(f"{key} = {value}")
else:
    print(f"Error: {response.status_code}")

So that completes a project to verify OTP code sent from /AuthKit. You can get the completed code below:

from dotenv import load_dotenv
import os # Used to use variables in the `.env` file
import requests

load_dotenv()

while True:
    method = input("RESTful Method (GET, POST): ")
    if method in ["GET", "POST"]:
        break
    else:
        print("Unsupported")

authkey = os.getenv("AVC_AUTHKEY")

if method == "POST":
    chatid = input("ChatID: ")
    params = {"authkey": authkey, "chatid": chatid}
else:
    params = {"authkey": authkey}

response = requests.request(method, "https://avc.vercel.app/api/telegram/otpVerification", params=params)

if response.status_code == 200:
    data = response.json()

    if method == "POST":
        verification_code = data.get("verificationCode")
        if verification_code is None:
            print("Error: 'verificationCode' not found in the response.")
        else:
            while True:
                usr_verifycode = int(input("Verification Code: "))

                if usr_verifycode == verification_code:
                    print("Verification Code correct")
                    break
                else:
                    print("Verification Code incorrect. Try again")
    else:
        for key, value in data.items():
            if isinstance(value, dict):
                var_name = key
                vars_list = ", ".join(value.keys())
                print(f"{var_name} = [{vars_list}]")
                for subkey, subvalue in value.items():
                    print(f"{subkey} = {subvalue}")
            else:
                print(f"{key} = {value}")
else:
    print(f"Error: {response.status_code}")