Skip to content

Instantly share code, notes, and snippets.

@manzoorwanijk
Last active December 23, 2023 01:42
Show Gist options
  • Star 14 You must be signed in to star a gist
  • Fork 9 You must be signed in to fork a gist
  • Save manzoorwanijk/ee9ed032caedf2bb0c83dea73bc9a28e to your computer and use it in GitHub Desktop.
Save manzoorwanijk/ee9ed032caedf2bb0c83dea73bc9a28e to your computer and use it in GitHub Desktop.
Google Script to bypass the blockage of Telegram Bot API from by webhost

WPTelegram Google Script

You can use this script to bypass the bans on Telegram API by different hosts. Simply send the request to this script instead of the Telegram Bot API after deploying it as a web app and allowing anonymous access.

Params

It accepts bot GET and POST requests with the following params

name type Description
bot_token String The Telegram Bot Token
method String Telegram Bot API method name e.g. sendMessage
args JSON Object The arguments/params for the API method e.g. {"chat_id":"123","text":"HelloWorld"}

How to Deploy

See the screenshots below 👇

  • Goto script.google.com and sign in if required.
  • Create a New project and give it a name you love :)
  • It should open a file (Code.gs by default). Remove the contents of this file.
  • Copy the contents of wptelegram-google-script.gs below and paste into your project file (Code.gs)
  • Click on Save (💾) or press Ctrl+S
  • Click "Deploy" at the top and select "New deployment" and it will open a popup
  • Inside the popup, click on "Select type" ⚙️ and choose "Web app"
  • In "Web app" Execute as", select "Me (<your email>)" [IMPORTANT]
  • In "Who has access", select "Anyone" [IMPORTANT]
  • Click on "Deploy" to open the Authorization box.
  • Click on "Authorize access" to authorize the script.
  • In the popup window select your Google Account.
  • If you see a warning "Google hasn't verified this app", it is fine to click "Advanced" and choose "Go to <app name> (unsafe)"
  • On the next screen, click "Allow".
  • After redirect, you should see "Deployment successfully updated"
  • Copy the "Web app URL" and paste it in your app or plugin
function doGet(e) {
if(typeof e !== 'undefined'){
return requestHandler(e);
}
}
function doPost(e) {
if(typeof e !== 'undefined'){
return requestHandler(e);
}
}
function requestHandler(e){
var res = handleRequest(e);
return ContentService.createTextOutput(res);
}
function handleRequest(e) {
if(typeof e.parameter.bot_token === 'undefined'){
return 'Error! Bot token not provided';
} else if(typeof e.parameter.method === 'undefined') {
return 'Error! Method name not provided';
}
var bot_token = e.parameter.bot_token;
var tg_method = e.parameter.method;
var data = {
"method": "post",
"muteHttpExceptions": true
}
if(typeof e.parameter.args !== 'undefined'){
var args = e.parameter.args;
data.payload = JSON.parse(args);
}
var res = UrlFetchApp.fetch('https://api.telegram.org/bot' + bot_token + '/' + tg_method, data);
return res.getContentText();
}
@Hyderme
Copy link

Hyderme commented Oct 25, 2022

It's not working for me please help

@alicompu
Copy link

alicompu commented Nov 24, 2022

Thank you so much.
Worked for me.

@ipjbenis
Copy link

ipjbenis commented Dec 3, 2022

function doGet(e){
var options = {
'method' : 'post',
'contentType': 'application/json',
// Convert the JavaScript object to a JSON string.
'payload' : e.postData.contents,
'muteHttpExceptions' : true
};
var response = UrlFetchApp.fetch('%webhook%', options);
Logger.log(response.getContentText());
}

function doPost(e){
var options = {
'method' : 'post',
'contentType': 'application/json',
'payload' : e.postData.contents,
'muteHttpExceptions' : true
};
var response = UrlFetchApp.fetch('%webhook%', options);
Logger.log(response.getContentText());
}

@ipjbenis
Copy link

ipjbenis commented Dec 3, 2022

TypeError: Cannot read property 'postData' of undefined
doGet @ Code.gs:6

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment