Drupal 8 form notifications to telegram bot

Going through the browser bookmarks I came across the site drupalfly.ru, where I looked through articles on Drupal and found an article on how the results of webforms reset telegram chat.

At the beginning, via @BotFather, we create a bot, write a name, description, put an image, get an api-key. Now we get the chat ID where our messages will be dumped. To do this, enter the address in the address bar

https://api.telegram.org/bot<token>/getUpdates

where <token> is our token, which is given to us when creating a bot. This token, in which case, can be re-generated in botfather.

After entering the address, we will open a page with chat data, where we are interested in the chat: id string:

Chat ID

This is the chat id where our bot will send the results of the forms.

Now we create a module on the site, create a custom folder in modules and a telegram_notification folder in it. Inside this folder, create two files - telegram_notification.info.yml and telegram_notification.module.

In the info file, add the info data of our module

name: Telegram Notification
description: Webform notification for Telegram
core: 8.x
type: module

In case the module will be used on Drupal 9, you need to add it after the core

core_version_requirement: ^8 || ^9

so that the system sees compatibility with the ninth version of the kernel.

Add the following content to the second file

<?php

/**
 * @param $form
 * @param \Drupal\Core\Form\FormStateInterface $form_state
 * @param $form
 * Implements hook_form_FORM_ID_alter()
 */

function telegram_notification_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {

    if($form['#id'] == 'webform-submission-price-add-form'){
        $form['actions']['submit']['#submit'][] = '_telegram_notification_form_submit';
    }
}

/**
 * @param $form
 * @param \Drupal\Core\Form\FormStateInterface $form_state
 * Callback telegram_notification
 */
function _telegram_notification_form_submit(&$form, \Drupal\Core\Form\FormStateInterface $form_state) {

    # Telegram data
    $telegram_token = 'bot'; // Your token, which is built using the bot + token template
    $telegram_id_chat = '';  // chat ID

    if (!empty($telegram_token) && !empty($telegram_id_chat)) {
        # Form data
        $data = $form_state->getValues();
        $data_result = array(
            'name' => '<b>Name:</b> ' . $data['name'],
            'email' => '<b>Email:</b> ' . $data['email'],
            'phone' => '<b>Phone:</b> ' . $data['phone']
        );
        # Send message
        $message = implode($data_result, '%0A');
        $send_message = fopen("https://api.telegram.org/{$telegram_token}/sendMessage?chat_id={$telegram_id_chat}&parse_mode=html&text={$message}", "r");
    }
}

Here in the line

if($form['#id'] == 'webform-submission-price-add-form'){

we set a condition, the results of which form we will send to the bot. The ID of our form is indicated.

Below we add the bot token and chat ID where the results will be sent.

After the line

$data_result = array(

we have a listing of our form fields and data that will be transmitted.

If we need to pass, for example, a static name of the form, in the form itself we add a hidden field with a default value, which will be equal to the one we need to display.

Then we clear the cache and try to send the form, the results of which should come to our bot.

In case we need to work with several forms, we can make several conditions on the id-forms and specify various functions, which we simply copy below and set our names and the output of our fields.

In addition, it may be necessary to send results to multiple users. The problem is that this api implies sending only one ID chat at a time. As a possible solution, add the bot to the group, give it admin rights. After that, we again execute the request to get an ID specialist. At first glance, the ID person may be very different and even have a minus at the beginning of the ID, we still copy and paste into $ telegram_id_chat = '';.

After all the manipulations, we should get a bot that sends messages from all the specified forms to the telegram group.

Perhaps you will have to change the value of the Group Privacy / Privacy mode and set it to disabled in the bot settings, through botfather, and then add it to the group again and assign it as an administrator.

 

 

 

 

 

 

 

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.
The comment language code.