Как отправить данные из html-формы в Telegram

How to send data from an html form to Telegram?

Hi everyone! Today we are going to understand how to send data from html form to Telegram?

You know what a feedback form is (I’m sure). They are used everywhere – even where they are not really needed. And you also know what Telegram is – a very convenient (probably the most convenient nowadays) messenger, and in general – a superstructure over the Internet. In general, I will not spread on the tree, I will tell you how to make a feedback form, the data from which will go to Telegram.
To do this, we will need:
1. Create a bot.
2. Create a group, to which we should join our freshly created bot from point 1
3. Create a feedback form that will be on our site. For example, the form will look like this (it’s just a form, it won’t do anything – you don’t have to press the button hard 🙂 ):


 

To be honest, I’m too lazy to bother right now, so the form consists of only one field, and a button 🙂
So, we have the form, and now let’s deal with the code that will send data from the html-form to Telegram. The code looks as simple as possible:

>

The logic of the code itself is as follows:
1. We know that we can work with Telegram by sending commands to its api via the browser address bar.
2. So all we need to do is:
– know the token of our bot (we get it at the moment of bot creation)
– know the chat_id (it is very easy to get the chat_id by following the link:
https://api.telegram.org/botTOKEN>/getUpdates, only instead of TOKEN put the token you got when you created your bot 😉
And yes – if you suddenly follow the link and get something like:
{“ok”:true,”result”:[]}
add your bot to your group, where the html form messages will be sent 😉 )
– generate a summary string that will contain:
— a link to Telegram API
— bot token
— command for the bot (in our case – sendMessage
— chat_id (where and how to get it I wrote a few lines above)
— text encoding mode
— and, actually, the text itself, which we need to get from the html-form.

Let’s do all of this:
– chat_id — check
– token — check
– link to Telegram API – there
– text that we get from html-form – is there (we get it here: $text = ‘Text from html-form: ‘.$_POST[‘input_text_box’];
It remains to generate the summary string for the browser:
$url = ‘https://api.telegram.org/bot’.$token.’/sendMessage?chat_id=’.$chat_id.’&parse_mode=html&text=’.$text;

and pass this string to the browser:

$handle = curl_init($url);
curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($handle);
header('Location: https://lavrynenko.com/form_to_telegram.html');

You can try all this in working form at this link 🙂 Go to it – you’ll see an html form where you enter text and submit.

However – you don’t have to follow the link… here is the form in a frame 🙂 :

I watch sometimes as people write something in the form, but don’t add to the group to see how the text goes there from the form. Reminder – here is the group: https://t.me/html_to_telegram – go here, and see how your texts sent from the form appear there 😉
IMPORTANT:
Want to make sure that the text sent via html-form reaches Telegram? No problem – join the group and watch the bot output everything you wrote in the html form in Telegram chat to this group.

HOW IMPORTANT: if suddenly your message didn’t get to the group – write to me in Telegram or mail. I will correct it 🙂

As always – if you have any questions, write to email, or to Telegram.

Support the Blog!

Running a blog takes a lot of effort, time, and passion. Your donations help improve the content, inspire new ideas, and keep the project going.
If you’ve enjoyed the blog’s materials, any support would mean the world to me. Thank you for being here! ❤️

PayPal Logo Donate via PayPal

Revolut Logo Donate via Revolut