Telegram bot for your HTML form

fetch(remoteUrl, {
    method: 'POST',
    headers: {
        "Content-Type": "application/x-www-form-urlencoded"
    },
    body: data
}).then( result => result.json() ).then(...)

Emails are a traditional way of delivery HTML forms from websites. But email goes along thorny route to an addressee and sometimes vanishes in spam filters. Recently a friend of mine learned that up to 40% of messages from his website’s application form didn’t reach inbox.

Instant message usually goes very straight way: sender → messenger → addressee. I also find messengers convenient and secure mean of staying connected. To fix the lost emails’ problem we made server duplicate emails with Telegram Bot to a group chat.

The article also covers personal messages’ delivery.

Create your bot

Find @BotFather in Telegram.

Botfather

Search yields more than one result. Be careful.

Enter /newbot command in @BotFather chat and follow simple instructions.

When complete you will get this message:

Bot creation success message

Now your new bot is ready to work. Save the API token that you see on the screen. You’ll need it soon. Later you will be able to get it this way: enter /mybots in the chat with @BotFather and select the bot from a list that will show you up. Then you get a menu which offers you among other things to get a token:

Bot menu

Chat ID

In order for Telegram to know who the bot is to send messages each chat has unique ID, whether it is a private chat or group.

To get the ID start the chat with bot. Then compose a special URI with your token and open it in browser:

https://api.telegram.org/bot719952928:AAEeL6OL2wtBjNaOAv5S9PN3etRv_FiLsh0/getUpdates

Paste your own token after https://api.telegram.org/bot, followed by /getUpdates. In response, you will get a JSON, where you have to find chat → id. Here is a sample:

JSON with chat id

To get a group ID the following way seems to be the simplest: open Overflow menu → Group info → manage group. This way:

Group management

Now open the invite link in browser:

Invite link

Don’t open it in application (JOIN GROUP). Instead open it in browser (OPEN IN WEB):

Open in browser

You will be redirected to a similar address:

https://web.telegram.org/#/im?p=g246586972

Characters after = sign are the group ID. Only replace g letter with - (minus) symbol. So in our case the group ID is -246586972.

Now you are ready to check how bot sends messages. Compose links like that:

· send message to a group:
https://api.telegram.org/bot719952928:AAEeL6OL2wtBjNaOAv5S9PN3etRv_FiLsh0/sendMessage?chat_id=-246586972&text=Hi%20buddy
· to a private chat:
https://api.telegram.org/bot719952928:AAEeL6OL2wtBjNaOAv5S9PN3etRv_FiLsh0/sendMessage?chat_id=97804534&text=Hi%20buddy

You may also test your bot with a form at a bottom of the post.

If you do not get messages, make sure you did everything right. At the time of writing everything works.

HTML form

Skipping HTML here. I don’t think anybody need mark-up, when reached the point of experimenting with forms. Only will show you new fetch API usage for sending forms:

fetch(remoteUrl, {
    method: 'POST',
    cache: 'no-cache',
    headers: {
        "Content-Type": "application/x-www-form-urlencoded"
    },
    body: data
}).then( result => result.json() ).then(...)

PHP form processor

PHP message sender essentially just forms a proper URI to the Telegram API. Thus things are pretty simple. Here is sample for the fetch showed above:

<?php
function sendMessage($token, $chatid, $message) {
    $url = "https://api.telegram.org/bot{$token}/sendMessage?chat_id={$chatid}&text=";
    $url .= urlencode($message);
    $ch = curl_init();
    $options = array(
        CURLOPT_URL => $url,
        CURLOPT_RETURNTRANSFER => true
    );
    curl_setopt_array($ch, $options);
    $result = curl_exec($ch);
    curl_close($ch);
    return $result;
}
$token = 'xxxxxxxxxx:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
$chatid = 'xxxxxxxxxxx';
$message = $_POST['message'] || 'our your source for message';
$response;
// now execute it:
$result = json_decode(sendMessage($token, $chatid, $message));
if(isset($result->ok) && $result->ok) {
    $response['body'] = 'Congratulations, the message was successfully sent';
} elseif (!$result->ok) {
    $response['error'] = true;
    $response['body'] = $result->error_code . ': ' . $result->description;
} else {
    $response['error'] = true;
    $response['body'] = 'Unknown error. Sorry :(';
};
printf(json_encode($response));

Now test your bot

Touch
to test your bot

To be continued

Viber and Messenger are next.