عکس AmirrezaNasiri
🌉 A bidirectional web-based proxy for Telegram Bot API request calls and webhook callbacks.PHP
موضوع‌ها
۰
فورک‌ها
۴
ستاره‌ها
۱۳
تاریخ ایجاد
۴ اردیبهشت ۱۳۹۸
آخرین بروزرسانی
۶ روز قبل
لایسنس
MIT License

Telegram Web Bridge

Licensed under the MIT License

Telegram Web Bridge

Telegram Web Bridge is a bidirectional, web-based, proxy-like application which can act as a middleman for Telegram Bot API requests (from your actual application to Telegram) and Webhooks (from Telegram to your actual application).
Built on Slim framework, light-weight and works smoothly.

What is the point?

This bridge can be useful in situations in which accessing Telegram Bot API is not possible (like being hosted in a country restricting/blocking access to Telegram).

Communication without proxy

Here are the problems come in:

  • We can't host our main application outside that region
  • We can't call Telegram Bot API
  • We can't receive their webhooks
  • ... but we still need Telegram!

Here this bridge comes in. You can host it in outside of the blocking region, use it to call APIs and receive webhooks.

Communication with proxy

But there are other solutions too!

Yes, there are plenty of other solutions too. However, not everyone can implement those solutions without technical knowledge.
This is a deployment-ready application built to be hosted even on a shared-hosting (since they are pretty cheap) without hassles. Simply download, extract on your shared web hosting, change few settings and done!

Requirements

The bridge application requires following conditions:

  • A shared-hosting, VPS or dedicated server
  • PHP version 7 or above with json extension enabled.
  • An installed SSL certificate. Can be Self-Signed but LetsEncrypts free certificates are much easier to setup! If you have a paid certificate, its good too.

Installation

You can either use deployment-ready archive or clone source code and build for yourself.

Visit following link and download the latest version:
Download Builds

Unzip the content of the build into a publicly accessible directory on your shared hosting. URL to that directory will be your appUrl in settings.

Jump to next section to continue installation.

Clone Repository (for advanced users)

Clone this repository on your server or local computer:

$ git clone https://github.com/AmirrezaNasiri/telegram-web-bridge

Use Composer to install dependencies:

$ composer install

Tests

Basic tests for API calls are available but for webhooks, no tests has been written yet. Create a Telegram bot for testing purpose and start chatting with it. Open tests/settings.php and configurate like this:
Enter the API token created for test bot:

'botApiToken' => '123456789:aBcdEfghIjklm_nOpqRsTuvWXyZaBcDEFjhi',

Specify your chat id (can be extracted from @chatid_echo_bot):

'chatId'      => '123456789,

Then run tests:

$ composer test

Configuration

All configurable settings are located and described in: src/settings.php

Basically, you only need to change appUrl, crtPath and webhookEndpoints settings.

Use the bridge!

Now that everything is ready, you just need to change the request URLs from https://api.telegram.org to what you've set in appUrl.
For example, following request

GET https://api.telegram.org/bot123456:ABC-DEF1234ghIkl-zyx57W2v1u123ew11/getMe

Must be changed to:

GET https://example.com/path-to/telegram-web-hook/bot123456:ABC-DEF1234ghIkl-zyx57W2v1u123ew11/getMe

To receive webhook callbacks, you need to fire setWebhook once again:

POST https://example.com/path-to/telegram-web-hook/bot123456:ABC-DEF1234ghIkl-zyx57W2v1u123ew11/setWebhook

The bridge will inject its own url and certificate parameters according to settings you've just set. So you should expect result of getWebhookInfo to be different than what you've sent in setWebhook request.
On every callback from Telegram, bridge will catch and forward it to the defined URL in the settings file.

Using an existence package to send Telegram requests?

No problem. You may be able to easily change target of requests from Telegram's URL to your bridge. Here are tips for several packages:

Contribution

This project welcomes community contributions.

License

Code is licensed under the MIT License.

See Also