https://github.com/dilshat/telegram-bot
A simple javascript telegram bot
https://github.com/dilshat/telegram-bot
bot javascript telegram
Last synced: 5 months ago
JSON representation
A simple javascript telegram bot
- Host: GitHub
- URL: https://github.com/dilshat/telegram-bot
- Owner: dilshat
- License: apache-2.0
- Created: 2020-04-28T09:07:01.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2020-09-14T08:06:37.000Z (almost 6 years ago)
- Last Synced: 2024-06-20T00:33:42.039Z (about 2 years ago)
- Topics: bot, javascript, telegram
- Language: Go
- Size: 6.18 MB
- Stars: 4
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: License.md
Awesome Lists containing this project
README
[](https://travis-ci.com/Dilshat/telegram-bot)
[](https://goreportcard.com/report/github.com/Dilshat/telegram-bot)
## 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`