Last Updated:

Contact form via TG bot

TECHTALKS Development

Telegram Setup

Joining Telegram

The first thing you're going to need to do, if you haven't already, is sign up for a Telegram account. It's free and all you'll need is a phone number. Sign Up Here Once you have your account, install it on your PC or Phone. I'll be providing instructions for and screenshots the PC version, but you should be able to follow along on mobile with no issues.

Creating the Bot

We'll need to create the bot that we'll use to send our messages. To manage our bots, we'll have to interact with the special bot Telegram has created. In the telegram app, drag from the left to extend the sidebasr of contacts if it's collapsed. In the searchbar at the top, enter @BotFather and click the bot that shows up.

  1. Click the Start button at the bottom of the chat and you should now be in a chat with the Botfather.
  2. Click the /newbot link in his message or type it into the chat. You should now be able to just follow BotFather's instructions and name your bot and give it a username ending in "bot". 
  3. Once your bot is created, copy your authorization token to a secure location.

Creating the Channel

  1. In the telegram app, open the sidebar with the hamburger menu in the top left corner and click New Channel  Next, give the channel a name (I would suggest <\Website Name> Notifications), and optionally a photo to easily recognize the channel at a glance.  
  2. Set the channel to private, as we don't want any kind of contact info being available to anyone we haven't authorized to join the channel.  
  3. When you're asked to add members to the channel, enter the name of your newly created bot in the search bar and select it.
  4. At this time, you can also add other people to the channel so they will get notified when new contact messages are submitted, but you'll have the option to do this at any time later as well.  Affirm the prompt to add the bot a channel admin. Finaly, you'll be able to set the bot's scope of permissions.
  5. Only enable posting messages, as that'll all we'll need for this channel.

Getting the Channel ID

Now that we have the channel, we'll need the channel ID. There isn't an easy way to just grab it yet, so we're going to use a telegram utility bot called JSON Dump Bot. Send a message in your channel, then right click it and select Forward. In the searchbar, search for JSONDumpBot, select it, and press start. This bot should reply with a message containing all the JSON data of the message we just forwarded. Ignore the first message (in response to the /start command). Look for the Chat ID and save it for later.

Testing our bot

The way this works, is we send a message to the channel with the bot as the user. This is done by making a GET request to the telegram API. The information about this request can be found in the Telegram API documentation:

You will need the bot token we got earlier as well as the channel ID

The request will look like this:

GET<token>/sendMessage?chat_id=<channel_id>&text=<Message Text>


Once you've constructed this link, paste it into your browser or desired HTTP client such as Insomnia or Postman If this works, you should recieve a message in the Telegram channel containing your text.

Building a contact form

With these steps completed, you'll be ready to implement the actual contact form.

This part can be done pretty much any way you want. All we have to do is add a place for users to enter some information and a button that will send the Telegram request.

Here's an example with the HTML form element, which require no script code.

<h2>Contact Us</h2>
<form action="">
    <input type="hidden" name="chat_id" value="-1001274004245">
    <p>Please provide your reqeust and a way for us to get back to you (email, phone number, Myspace ID, etc)
    <input type="text" name="text" value="Hello, world!">
    <input type="submit">

Enhanced version w/ JS

If you want to customize your contact form further, you can build it like a regular form, then use javascript to combine the values of the form elements into a single text string then send the request. This allows for more forum inputs as well as displaying the response status of the request to the user.

Example of the request with Javascript:

<form id="messageForm">
    <input type="text" name="Name" required>
    <input type="email" name="email" required>
    <select name="Goal" required>
        <option value="qoute">Get a Quote</option>
        <option value="purchase">Make a purchase</option>
        <option value="question">Other questions?</option>
    <textarea name="message" required></textarea>
    <input type="submit">
    <p id="status"></p> <!-- This is for displaying if the message sent correctly -->

    const chat_id = '-1001274004245', botID = 'bot1690645634:AAFV31gPaGrOsY90VRdeZuxfeZqfWkfa3D0';
    const telegramURL = `${botID}/sendMessage`;
    document.querySelector('#messageForm').addEventListener("submit", async e => { // When the user submits the form
        e.preventDefault(); // Don't submit
        let text = JSON.stringify( // Convert the form data to a string to send as our Telegram message
            Object.fromEntries(new FormData(, // Convert the form data to an object.
        null, 2); // Prettify the JSON so we can read the data easily
        const sendMessage = await fetch(telegramURL, { // Send the request to the telegram API
            method: 'POST',
            headers: {"Content-Type": "application/json"}, // This is required when sending a JSON body.
            body: JSON.stringify({chat_id, text}), // The body must be a string, not an object
        const messageStatus = document.querySelector('#status');
        if (sendMessage.ok) // Update the user on if the message went through
            messageStatus.textContent = "Message Sent!";
            messageStatus.textContent = "Message Failed to send :( " + (await sendMessage.text());; // Clear the form fields.

Enhancing Security

These are both very simple ways to have your users send requests and contact you, but there is a potential security issue here.

To make the request, all you need is the bot access token, meaning anyone that has it can make requests to send us notifications. Fortunately, the bots are limited in their scope. So, they can't send messages to anyone, but if someone has the bot access token and channel ID, they could very easily spam requests to bombard you with notifications. This wouldn't be dangerous, but it would likely be very annoying. Additionally would probably flag Telegram's systems and they might revoke access to your bot, cutting off access for your users to contact you.

The solution

The best way to deal with this is to not store your bot access token in your client-side code. Instead, we can set up an intermediary service on the backend to send the telegram request. This can be with any backend technology such as PHP or Node.js and express. The way this works is: The user submits the form, the data is sent to the backend (your server), and then the server sends the request to telegram. This way, you can store the access token in the server code as well as do things like rate limiting to prevent spam.