Telegram bots can be made with various programming languages:
There are two basic knowledge that must be understood:
- How to Make an HTTP Request and Webhook.
- How to Parse JSON from a Webservice.
But, now the problem is where do we put the codes?
"Rent a server ..."
The flakes are too expensive, if only for learning.
"What if you use it for free."
yep, that's the answer.
In this paper, we will learn how to make Telegram Bots using the Webhookmethod using the Hook.io service .
So that later we will be able to make a bot that is ready to use.
There are two main discussions in this guide:
- How to Create Bots in BotFather
- How to Make Services for Machine Bot on Hook.io uses javascript programming language.
Let's start…
Materials needed:
- Telegram Bot Token - key to accessing bots.
- Hook.io account - please register first !
Make APIs and Tokens
On the command bot (
/
) button, select /newBot
.
After that, please reply with the bot name. Example: "Mr. Farmer".
Next, we will be asked to provide a username for the bot.
Each username bot must end with the word "bot" behind it.
after that, we will get the bot bot token code.
Save the token, because we will use it to connect it to Hook.io.
Make a Service at Hook.io
After you create an account at Hook.io, please create a new service.
Click here to make it.
Fill in the name of the hook to be made, for example "farmer".
Click Create to make it.
Creating Environment Variables (ENV)
ENV
We will use the environment variable ( ) to store the bot token so that we don't type the token in the code. Because tokens are confidential.Creating a Code for Bot Control
Treat the Hook Source code :
Please change the code (Hook Source) with this code.
module['exports'] = function petaniBot(hook) {
// impor modul request
var request = require('request');
// buat atau kirim request ke bot telegram
request.post('https://api.telegram.org/bot' + hook.env.token_petani_bot + '/sendMessage')
.form({
'chat_id': hook.params.message.chat.id,
'text': 'Anda mengirim: ' + hook.params.message.text
});
}
The function of the code above will only send a request to the Telegram Bot, if there is a request or chat entered.
{
"chat_id": hook.params.message.chat.id,
"text": "Anda mengirim: " + hook.params.message.text
}
In order to be sent, we need a bot token.
The bot token is already in a variable
token_petani_bot
, so it's only usable:request.post('https://api.telegram.org/bot' + hook.env.token_petani_bot + '/sendMessage').form(<data>);
The control code is ready ...
Next, we will connect Hook.io with the bot.
How to Connect a Telegram Bot with Hook.io
To connect a bot to a hook, please arrange the URL to be like this:
https://api.telegram.org/bot<TOKEN>/setWebhook?url=https://hook.io/<hook-user>/<hook-name>
Please replace:
<TOKEN>
with your bot token<hook-user>
with your hook.io username<hook-name>
with the name of the service / service hook that you made
Example:
https://api.telegram.org/bot247322285:AADCfe2WSna7sclLalfBq5VCmn3yTzii_zQ/setWebhook?url=https://hook.io/ardianta/petani/
After that, please open the URL via the browser.
If you get a reply like this, then the bot is successfully connected with the hook service.
Trial:
Conclusions and recommendations
We can control the Telegram bot with the code stored in Hook.io.
We can determine how bot behavior through code.
In the example above, the bot has successfully received and sent replies.
Suggestions for further development:
- Add logic so that bots are getting smarter.
- Add an artificial intelligence algorithm.
- Access third party APIs, such as the API for checking weather.
- Using the Simsimi API so that the bot can talk like Simsimi .
- ____ (add yourself) .
Thank you for following this guide ...
0 Komentar untuk "How to Make a Telegram Bot with Hook.io Service"
Silahkan berkomentar sesuai artikel