Create telegram bots never been so easy.
Serve as a starting point to develop telegram personal or public bots like :
- List my movies
- List my schedule
- Get random names
- Or whatever you want
- Node.js >=14
- Telegram account
- Telegram Token (BotFather)
-
Start new telegram chat with botFather and send the following commands:
-
/start
-
/newbot
-
Enter : Name of your Bot
-
Enter : Id of your Bot
This step , will return to you the token. This is required to register and interact with your bot. Save it!!!
- Clone the template : https://github.com/jrichardsz/telegram-bot-4all-demo.git
- This template has a command sample called /author who returns the name of creator of this tool
You need to deploy your bot (this source code) in a public server or domain: using some free or private service like: Heroku, Openshift, https://zeit.co/now, etc
Example with heroku:
- Create an account in heroku (for instance)
- Create a configuration variable called : TELEGRAM_BOT_TOKEN with token gotten in previous step.
- Push and save the public url of this heroku application.
If your bot is deployed and has a url like:
You must register it, executing this:
curl -F "url=https://my_awesome_bot.herokuapp.com/new-message" https://api.telegram.org/bot<your_api_token>/setWebhook
If you followed all the steps without errors, you could use this command with your own bot:
If you don't have any response, check de logs or review the telegram father configurations.
- We will create a command who return the data of the current day
- Create a module inside of commands folder. The doit method is mandatory
function Today() {
this.doit = (commandArguments, chatMessageObject) => {
var today = new Date();
return today.toISOString().substring(0, 10);
}
}
module.exports = Today;
The framework will inject you two variables: (commandArguments, chatMessageObject)
in the doit
method
commandArguments
The parameters received with your command. If the chat was: /buy_coffee param1 param2
, you will receive: param1 param2
chatMessageObject
The entire json sent by telegram bot internal engine:
{
"message": {
"from": {
"id": 123456,
"is_bot": false,
"first_name": "JohnDoe",
"last_name": "ENL",
"username": "john_doe"
},
"chat": {
"id": 87865786,
"title": "My group or chat title",
"type": "group"
},
"text": "/author param1 param2"
}
}
You could use these variables for what you want to do with your bot command.
- That's all. Redeploy your bot and you will have a new command, ready to use it in the chat with
/today
- Emulate telegram service for local tests
- Add self webhook endpoint
- Add complex feature : receive images
- Add complex feature : receive form data
- Add complex feature : payments
JRichardsz |