Git Product home page Git Product logo

telegram-bot's Introduction

Build Status Go Report Card

A simple javascript telegram bot

Sometimes a simple bot is needed to be built quickly. This bot allows to write logic of bot in javascript. The script can use embedded methods to implement various functionality: sending messages with attachments, custom keyboards and inline options. You can store objects in cache and in database (mysql and postgres are supported). You can code conversational bot using session like approach. See examples in scripts/*.js.

Embedded functions:

set(key, value) - puts a json object to cache indexed by key

var sess = {step:0}
set("session", sess)

get(key) - returns a json object from cache by key

var sess = get("session")
if (sess) {
  console.log(sess.step) 
}

del(key) - removes a json object from cache by key

del("session")

send(...) - sends a message to user

send("Hi") // sends a simple text message

send("Choose one", [["option1", "option2"]]) // sends a text message with custom keyboard

send("Choose one", [["option1"], ["option2"]]) // sends a text message with custom keyboard with button groups each on a new line

send("Download this file", null, "the-file.ext") // sends a text message with a file from attachments directory

send("Forwarded this file", null, "{fileID}:photo") // forwards a file, that is already uploaded to the chat by its fileID and type (photo, video, audio) separated by colon

send("Forwarded this file", null, "{fileID}") // forwards a file, that is already uploaded to the chat by its fileID as a generic document

send("hi Admin", null, null, adminId) // sends a message to the specified user (by telegram id)

send("Test", [{ "One": "option-1", "Two": "option-2", "Three": "option-3" }]) - sends a message with inline keyboard. When user presses a button, script will have access to a callback object

send("Test", [{ "One": "option-1", "Two": "option-2" }])

send("Test", [{ "One": "option-1"}, {"Two": "option-2" }]) // button are displayed in groups, each on a new line

 ...

//process user click on a button
console.log("Option " + callback.Data + " selected")

if button data is a valid URL, clicking the button will not trigger callback but rather attempt to navigate the specified url

getFileLink(fileID) - returns link to file download by its fileID. It is no recommended to share the link with users, since it contains bot token. Is is supposed to be used by bot admins

if (message.Photo && message.Photo.length > 0) {
  console.log("Photo received:" + getFileLink(message.Photo[message.Photo.length - 1].FileID))
}

replaceOptions - replaces inline keyboard

replaceOptions(message.Chat.ID, message.MessageID, [{ "Three": "option-3", "Four": "option-4" }] )

editMessage - updates message text and inline keyboard. Currently it is possible to edit messages with inline keyboard only

var id = send("Original message", [{ "One": "option-1", "Two": "option-2", "Three": "option-3" }])
sleep(1000)
editMessage(message.Chat.ID, id, "Edited message", [{ "Three": "option-3", "Four": "option-4" }])

deleteMessage - deletes message

deleteMessage(message.Chat.ID, message.MessageID)

prompt(text, attachment, userId) - sends message prompting user to reply to it (force reply). When bot is used in group chats, use this method to allow bot recieve user messages and respond to them, because bot can not "see" ordinary text messages in group chats, it "sees" only reply messages

prompt("What is your phone number") 

...

//process user reply
if (message.ReplyToMessage) {
  console.log(message.ReplyToMessage.Text + " -> " + message.Text)
}

How to use:

  • Implement logic in scripts/*.js files
  • In file .env set TELEGRAM_TOKEN to your bot token and SCRIPTS to your scripts
  • Build bot go build and run ./telegram-bot

Run in Docker:

  • Implement logic in scripts/*.js files
  • In docker-compose.yml, set TELEGRAM_TOKEN to your bot token and SCRIPTS to your scripts
  • Run docker using docker-compose up -d

telegram-bot's People

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.