Blog for Learning

This blog contains lesson material, material summary, questions and answers, definitions, types and examples, and others.

Advertisement

Powered by Blogger.

Visitors

Flag Counter

Pages

How to Make a Telegram Bot with Hook.io Service

Tutorial on Creating a Telegram Bot
Telegram bots can be made with various programming languages:
Can PHP , Javascript , Java , Python , Ruby , and so on.
There are two basic knowledge that must be understood:
  1. How to Make an HTTP Request and Webhook.
  2. 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.
One of the services hosting free supply WebHook is Hook.io .
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.
Bot Relations with Hook.io
There are two main discussions in this guide:
  1. How to Create Bots in BotFather
  2. How to Make Services for Machine Bot on Hook.io uses javascript programming language.
Let's start…

Materials needed:

  1. Telegram Bot Token - key to accessing bots.
  2. Hook.io account - please register first !

Make APIs and Tokens

Please open @botfather to create a new bot.
On the command bot ( /) button, select /newBot.
Register for a new bot
After that, please reply with the bot name. Example: "Mr. Farmer".
Give the name of the bot
Next, we will be asked to provide a username for the bot.
Each username bot must end with the word "bot" behind it.
Create a username for the bot
after that, we will get the bot bot token code.
Get the Bot token
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".
Make a webhook for bots
Click Create to make it.

Creating Environment Variables (ENV)

ENVWe 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.
Please go to https://hook.io/env to create environment variables.
Create environment variables

Creating a Code for Bot Control

Treat the Hook Source code :
Default webhook 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.
The contents of the submission are JSON: (see Bot API documentation )
{
    "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

Bot Relations 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.
Linking Bots with Webhook

Trial:

Please open or chat with the bot that was created, https://t.me/petani_bot .
Bot Test

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:
Thank you for following this guide ...
Don't forget to join the @partanikode channel on the Telegram to get the latest information.
0 Komentar untuk "How to Make a Telegram Bot with Hook.io Service"

Silahkan berkomentar sesuai artikel

 
Template By Kunci Dunia
Back To Top