PHPackages                             dangujba/teleotp - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Authentication &amp; Authorization](/categories/authentication)
4. /
5. dangujba/teleotp

ActiveLibrary[Authentication &amp; Authorization](/categories/authentication)

dangujba/teleotp
================

A PHP library for Telegram-based OTP authentication.

00PHP

Since Dec 2Pushed 1y ago1 watchersCompare

[ Source](https://github.com/Dangujba/TeleOTP)[ Packagist](https://packagist.org/packages/dangujba/teleotp)[ RSS](/packages/dangujba-teleotp/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependenciesVersions (1)Used By (0)

TeleOTP Library
===============

[](#teleotp-library)

The **TeleOTP** is a PHP library designed to interact with a Telegram-based OTP (One-Time Password) gateway system, allowing for the generation, sending, and verification of OTPs via Telegram. This library offers a set of methods to manage OTP functionality, including API communication, request handling, and response parsing.

Table of Contents
-----------------

[](#table-of-contents)

1. [Overview](#overview)
2. [Installation](#installation)
3. [Usage](#usage)
    - [Class Initialization](#class-initialization)
    - [Setting Parameters](#setting-parameters)
    - [Sending OTP](#sending-otp)
    - [Verifying OTP](#verifying-otp)
    - [Revoking OTP](#revoking-otp)
    - [Checking Sending Ability](#check-ability)
4. [Methods](#methods)
    - [Constructor](#constructor)
    - [Setters and Getters](#setters-and-getters)
    - [Sending and Verifying OTP](#sending-and-verifying-otp)
    - [Utility Methods](#utility-methods)
5. [Error Handling](#error-handling)
6. [License](#license)

Overview
--------

[](#overview)

The **TeleOTP** library interacts with the Telegram OTP gateway API to facilitate sending and verifying OTPs. You can set parameters such as the phone number, code length, callback URL, and more. The library handles communication with the Telegram API and provides methods for both sending OTPs and verifying their validity.

**Note**: For testing case you can simply use default phone number provided during registration which linked together with the [Telegram Gateway API Token](https://gateway.telegram.org)

Installation
------------

[](#installation)

You can install the `TeleOTP` library either manually or via Composer.

### Option 1: Install via Composer

[](#option-1-install-via-composer)

The recommended way to install `TeleOTP` is through Composer. To do so, run the following command:

```
composer require dangujba/teleotp
```

This will download the package and its dependencies to your project. Once you've installed your package via Composer, using it in your project is straightforward. Here's how you can set it up and use it:

### Steps for Using `TeleOTP` After Installing via Composer

[](#steps-for-using-teleotp-after-installing-via-composer)

1. **Install via Composer**
    If you haven't already done this, install the package by running the following command in your project directory:

    ```
    composer require dangujba/teleotp
    ```
2. **Include Composer's Autoloader**
    Composer automatically generates an autoloader that can be included to access all installed packages. At the top of your PHP script, include the Composer autoloader:

    ```
    require 'vendor/autoload.php';
    ```

    This will load all dependencies installed via Composer, including `TeleOTP`.
3. **Instantiate and Use `TeleOTP`**
    After including the autoloader, you can instantiate the `TeleOTP` class and start using it:

    ```
    use TeleOTP\TeleOTP;

    // Instantiate the class with your API token
    $teleOtp = new TeleOTP('YOUR_API_TOKEN');

    // Example usage
    $teleOtp->sendOtp('PHONE_NUMBER'); // Replace with an actual phone number
    ```

    Ensure that `YOUR_API_TOKEN` is replaced with your actual API token for the service you’re using with `TeleOTP`.

### Example Code

[](#example-code)

```
// Include Composer's autoloader
require 'vendor/autoload.php';

// Use the TeleOTP class (adjust the namespace as needed)
use TeleOTP\TeleOTP;

// Instantiate the TeleOTP class
$teleOtp = new TeleOTP('YOUR_API_TOKEN');

// Set code i.e your otp instead of letting telegram to generate it for you
$teleOtp->setCode('666666');

// Set callback url (optional)
$teleOtp->setCallbackUrl('https://github.com');

// Set custom payload (optional)
$body = [
   'id' => '1234',
   'purpose' => 'registration'
];
$teleOtp->setPayload($body);

// Example function to send OTP
$teleOtp->sendOTP('PHONE_NUMBER'); // Provide the phone number to send OTP to e.g +2341234567
```

### Option 2: Manual Installation

[](#option-2-manual-installation)

If you prefer not to use Composer, you can manually include the `TeleOTP.php` file in your project.

1. **Download or clone the repository:**

    You can either download the ZIP file from GitHub or clone the repository using Git:

    ```
    git clone https://github.com/Dangujba/TeleOTP.git
    ```
2. **Include the `TeleOTP.php` file:**

    Include the `TeleOTP.php` file in your project:

    ```
    require_once 'path/to/TeleOTP.php';
    ```

    Replace `path/to` with the actual path to the `TeleOTP.php` file in your project.
3. **Instantiate the `TeleOTP` class:**

    Once included, instantiate the `TeleOTP` class and set up your API token:

    ```
    $teleOtp = new TeleOTP('YOUR_API_TOKEN');
    ```

### Configuration

[](#configuration)

Make sure to replace `'YOUR_API_TOKEN'` with your actual API token provided by the [Telegram Gateway API](https://gateway.telegram.org).

---

For more details on usage and available features, refer to the documentation or check the examples in the repository.

### Explanation:

[](#explanation)

- **Option 1 (Composer Installation):** Instructions on installing via Composer with the `composer require` command.
- **Option 2 (Manual Installation):** Instructions for cloning or downloading the repository and including the `TeleOTP.php` file manually.
- The rest of the file explains how to instantiate the class and configure the API token.

Ensure to replace `your-username/teleotp` with the actual name of your Composer package.

Usage
-----

[](#usage)

### Class Initialization

[](#class-initialization)

To initialize the class, create a new instance of `TeleOTP`. Optionally, you can pass an API token for authentication.

```
$teleOTP = new TeleOTP('YOUR_API_TOKEN');
```

### Setting Parameters

[](#setting-parameters)

You can set various parameters using the provided setter methods.

```
$teleOTP->setCodeLength(6); // Code Length must be provided if setCode not used
$teleOTP->setCallbackUrl('https://example.com/callback');
$teleOTP->setPhoneNumber('+234567890');
// setCode will be used if dont want to use telegram default OTP
$teleOTP->setCode('223344'); // 6 to 8 digits
```

### Sending OTP

[](#sending-otp)

To send an OTP, simply call the `sendOTP` method, passing in the phone number (if not already set).

```
$response = $teleOTP->sendOTP('+1234567890');
```

This will send an OTP to the specified phone number and return the API response.

### Verifying OTP

[](#verifying-otp)

After receiving the OTP, you can verify it using the `validateCode` method. Pass the request ID and the code entered by the user.

```
// If not request id provided, last request id will be used.
$verificationResult = $teleOTP->validateCode([
    'request_id' => 'some_request_id',
    'code' => '123456'
]);
```

### Revoking OTP

[](#revoking-otp)

Use this method to revoke a verification message that was sent previously. Returns True if the revocation request was received. However, this does not guarantee that the message will be deleted. For example, it will not be removed if the recipient has already read it.

```
$teleOTP->revokeCode();
```

### Check Sending Ability

[](#check-sending-ability)

Use this method to optionally check the ability to send a verification message to the specified phone number. If the ability to send is confirmed, a fee will apply according to the pricing plan. After checking, you can send a verification message using the sendOTP method, providing the true from this response.

Within the scope of a request\_id, only one fee can be charged. Calling sendOTP once with the returned request\_id will be free of charge, while repeated calls will result in an error. Conversely, calls that don't include a request\_id will spawn new requests and incur the respective fees accordingly. Note that this method is always free of charge when used to send codes to your own phone number.

```
$teleOTP->checkAbility(); // if want to use already provided phone number else,

$teleOTP->checkAbility('+234123456');
```

Methods
-------

[](#methods)

### Constructor

[](#constructor)

```
public function __construct($token = '');
```

- **Parameters:**

    - `$token`: (string) The API token for authentication.
- Initializes the class with an optional API token.

### Setters and Getters

[](#setters-and-getters)

- **setToken($token)**

    - Sets the API token.
- **getToken()**

    - Returns the current API token.
- **setPhoneNumber($phoneNumber)**

    - Sets the phone number for OTP operations.
- **getPhoneNumber()**

    - Returns the phone number.
- **setCodeLength($code)**

    - Sets the OTP code length (between 4 and 8 digits).
- **getCodeLength()**

    - Returns the OTP code length.
- **setRequestId($requestID)**

    - Sets the request ID for tracking the OTP request.
- **getRequestId()**

    - Returns the current request ID.
- **setCallbackUrl($callbackUrl)**

    - Sets the callback URL for responses.
- **getCallbackUrl()**

    - Returns the callback URL.

### Sending and Verifying OTP

[](#sending-and-verifying-otp)

- **sendOTP($phoneNumber = '')**

    - Sends an OTP to the provided phone number (or the phone number set in the object).
- **validateCode(array $params)**

    - Verifies the OTP code entered by the user.
    - **Parameters:**
        - `$params`: (array) Contains the `request_id` and optional `code` for validation.
    - **Returns:**
        - `true` if the code is valid, otherwise a relevant status message (e.g., 'Expired', 'Invalid').

### Utility Methods

[](#utility-methods)

- **postRequest($request, $endPoint)**

    - Sends a POST request to the API endpoint with the specified parameters.
- **getResponse()**

    - Returns the last raw response received from the server.
- **getRawResponse()**

    - Returns the decoded response from the last API call as an associative array.
- **getRequestCost()**

    - Retrieves the cost of the last request from the API response.
- **getRemainingBalance()**

    - Retrieves the remaining balance from the last response.
- **getOTPStatus()**

    - Retrieves the status of the OTP, such as 'Sent', 'Read', or 'Revoked'.

Error Handling
--------------

[](#error-handling)

The library will throw exceptions for invalid inputs or missing required parameters. For example:

- `Exception("Phone Number is required!")` if no phone number is set or passed.
- `Exception("TTL must be between 60 and 86400 seconds.")` if the TTL is outside the valid range.

Make sure to wrap your method calls in try-catch blocks to handle any errors gracefully.

```
try {
    $teleOTP->sendOTP('1234567890');
} catch (Exception $e) {
    echo 'Error: ' . $e->getMessage();
}
```

License
-------

[](#license)

The TeleOTP library is released under the MIT License. See the LICENSE file for more information.

###  Health Score

14

—

LowBetter than 2% of packages

Maintenance32

Infrequent updates — may be unmaintained

Popularity0

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity16

Early-stage or recently created project

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

### Community

Maintainers

![](https://www.gravatar.com/avatar/0ba523d29e3dfcddd4a0ef528f7612fd7e58fedacaa3d256fbb14a39a39d61ec?d=identicon)[Dangujba](/maintainers/Dangujba)

---

Top Contributors

[![Dangujba](https://avatars.githubusercontent.com/u/24245211?v=4)](https://github.com/Dangujba "Dangujba (12 commits)")

### Embed Badge

![Health badge](/badges/dangujba-teleotp/health.svg)

```
[![Health](https://phpackages.com/badges/dangujba-teleotp/health.svg)](https://phpackages.com/packages/dangujba-teleotp)
```

###  Alternatives

[namshi/jose

JSON Object Signing and Encryption library for PHP.

1.8k99.6M101](/packages/namshi-jose)[league/oauth1-client

OAuth 1.0 Client Library

99698.8M106](/packages/league-oauth1-client)[bezhansalleh/filament-shield

Filament support for `spatie/laravel-permission`.

2.8k2.9M88](/packages/bezhansalleh-filament-shield)[gesdinet/jwt-refresh-token-bundle

Implements a refresh token system over Json Web Tokens in Symfony

70516.4M35](/packages/gesdinet-jwt-refresh-token-bundle)[league/oauth2-google

Google OAuth 2.0 Client Provider for The PHP League OAuth2-Client

41721.2M117](/packages/league-oauth2-google)[illuminate/auth

The Illuminate Auth package.

9327.3M1.0k](/packages/illuminate-auth)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
