How to send a request from a website to Telegram?

How to send a request from a website to Telegram?

How to send an application from a website to Telegram?

We all know that very often a feedback form is needed on a wide variety of sites. There, a person can leave their contact information so that people representing the site can contact him. And yes, this is often sent to regular mail (e-mail). However, now, thanks to the open and uncomplicated Telegram API, you can safely configure sending the same messages to it. What for? It is faster and much more convenient. Immediately, in a special chat, you will receive a request from the user.

  1. First, in the search for Telegram contacts, enter @BotFather… This is the “father” of bots, which allows you to perform some manipulations with them. Including creating a bot (all screenshots are shown using the example of Telegram for Windows)
  2. Push the button Start in a chat with BotFather, after which a list of available commands will drop out to our attention. We can find the command we need and click, or we can write directly to the chat: “/ newbot”, without quotes naturally
  3. Next, we will be asked to enter the name of the bot. Be careful and selective, because Telegram works all over the world and a huge number of names are already taken Create a bot and come up with a name for it
  4. After successfully entering the name, you will need to enter the username of the bot, by which it can be found in the search Enter the username of the bot
  5. When you select the correct name for the bot, you will receive the last necessary message from BotFather, containing the so-called “token”. This token will be useful to you for further actions (the token can be seen in the image above)
  6. Next, you need to create a group chat (New Group)… We call this chat whatever we want, and add the already created bot there through the search
  7. The next step is to connect the bot to the chat. This is done by the team “/ join @maxgrbot_bot” in your created chat.
  8. Then by clicking on “maxgrbot_bot” (i.e. the name of your bot, you will have it differently), go to your personal chat with him and press the button Start… This completes the work with Telegram.

Working with a form

Now you need to create a form and mark it up correctly to work with sending via Php. Below is an example of a typical HTML form:

<form action="telegram.php" method="POST">
<legend>Заголовок формы</legend>

<div class="form-group">
<input type="text" class="form-control" id="" name="name" placeholder="Введите имя">
</div>

<div class="form-group">
<input type="text" class="form-control" id="" name="phone" placeholder="Введите телефон">
</div>

<div class="form-group">
<input type="text" class="form-control" id="" name="email" placeholder="Введите email">
</div>

<div class="form-group">
<textarea name="msg" placeholder="введите сообщение"></textarea>
</div>

<button type="submit" class="btn">Отправить форму</button>
</form>

Important nuances:

  • The form action must be written telegram.php
  • The button itself must be of type submit
  • Also, the form must have method="POST"
  • Each input must have an attribute name

PHP script

Next, you need to write a simple php script for our sending to work:

<?php

$name = $_POST['name'];
$phone = $_POST['phone'];
$email = $_POST['email'];
$msg = $_POST['msg'];
$token = "1007513873:AAHRuGZ8Cr1wULSsMevQi6S1KtbdwbSyfiU";
$chat_id = "-399030555";
$arr = array(
'Имя пользователя: ' => $name,
'Телефон: ' => $phone,
'Email' => $email,
'Сообщение:' => $msg
);

foreach($arr as $key => $value) {
$txt .= "<b>".$key."</b> ".$value."%0A";
};

$sendToTelegram = fopen("https://api.telegram.org/bot{$token}/sendMessage?chat_id={$chat_id}&parse_mode=html&text={$txt}","r");

if ($sendToTelegram && $sendToTelegram2) {
header('Location: thanks.html');
} else {
echo "Error";
}
?>

Important nuances:

  • Into variables $name, $phone etc. you need to enter the values ​​of the attribute name from html
  • Into a variable $token write the token you created earlier at BotFather

What to do with chat_id

Of all that we need to fill, one field remains – $chat_id… You need to know it from somewhere in order to fill it. Here we will be helped by adding a special bot to our group – @RawDataBot… It will allow us to easily get the necessary data directly in the chat, including chat_id

Conclusion

And then all that remains is to send our form. I hope I could help you, and if a text description is not enough for you, at the beginning of the article there is a video from my YouTube channel.

Good luck!

Leave a Reply

Your email address will not be published. Required fields are marked *