Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@senselessnick
Last active September 25, 2022 18:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save senselessnick/64944ce725a0b883eb97b9b12a8a95f0 to your computer and use it in GitHub Desktop.
Save senselessnick/64944ce725a0b883eb97b9b12a8a95f0 to your computer and use it in GitHub Desktop.
Free AppsScript-hosted telegram bot

Donate:

Donate: one ☕, two 💸

Order bot:

Order here ✨ or here 🖥️

How to use this shit:

import library 1Qn4F0nOiboBPZx0IWqBRBffD3ItfPFKgyH9VJwITj8arLhdyhd6uxXxC to your project

Set up bot TOKEN, WEBHOOK url and HANDLERS like this:

const TOKEN = '777777777:AABBBCCCDDDEEEFFFGGGHHHIII-1WCVKD8h',
    WEBHOOK = 'https://script.google.com/macros/s/AKgdsfghbyU-XKv_mTgdsfgzKT_sdfgVCIvXbgiVbY4-WKZFrt_dghjLKNxlsadadsfBXLp/exec',
    HANDLERS = [{
        event: TBOT2.voice,
        handler: (bot) => {
        bot.apiRequest("sendMessage", {
                text: "SHUT UP!",
                chat_id: bot.update.message.chat.id,
                reply_to_message_id: bot.update.message.message_id
            })
        }
    }]

Handler is object containing 2 props: event (there "voice" event) and function that will triggerred via this event (see full events list by editor autocomplete)

Any non-predefined event will turned to bot /command, eg {event: 'hello'} (handler just skipped there, bad idea) creates /hello command

Handler function have only one argument: bot itself. If you need arguments of /command arg1 arg2 etc, take it from bot.args[]

Handler can be anonymous (like above) or not, example:

function blahblah(mybot) {
  //do some stuff with mybot, SpreadSheet table, doc, all you want to do
}
HANDLERS = [{
    event: 'video',
    handler: blahblah
}]

Inside you handler you have access to important property update and method apiRequest

apiRequest requires two args: method name and method params. Params is just {Object} containing telegram API parameters. It's easy like JSON, see first example above

Next, create function doPost:

function doPost(e) {
    //e it's http postdata from telegram server
    TBOT2.HANDLE(TOKEN, WEBHOOK, HANDLERS, e)
}

Next, deploy your app as public webapp (with anyone access and your name run), and copy link. This is your WEBHOOK now. Then, set up webhook, for example:

function sw() {
    TBOT2.WEBHOOK(TOKEN, "your deployment url from prev step")
}

...and run it via Apps Script Editor integrated debugger.

Done! Do not forget, after code modification you must deploy your app again (it's more handy to "Manage deployments" -> "Edit" -> "New version", then you no need to update webhook url)

const TOKEN = 'INSERT-BOT-TOKEN-HERE',
WEBHOOK = 'Apps script deployment url',
HANDLERS = [
{
event: TBOT2.voice,
handler: (bot) => {
bot.apiRequest("sendMessage", {
text: "SHUT UP",
chat_id: bot.update.message.chat.id,
reply_to_message_id: bot.update.message.message_id
})
}
},
]
function init() {
console.log(TBOT2.WEBHOOK(TOKEN, WEBHOOK))
}
function doPost(e) {
TBOT.HANDLE(TOKEN, WEBHOOK, HANDLERS, e)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment