K
K
KunaRaiden2020-10-13 11:11:04
PHP
KunaRaiden, 2020-10-13 11:11:04

How to send different data from a form on a website to telegram chats?

Hello!
How can I send applications from the form on the site to 2 different telegram chats, with different data?
In the first telegram chat, the full phone number, and in the second, the last 4 digits. If the matter is only in the handler, then here is the code itself. Or is the JavaScript logic involved here?
Please help me figure it out, thanks.

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
  $args = filter_input_array(INPUT_POST, FILTER_SANITIZE_STRING);
  if (!empty($args['uname'])){
    $arr = [
      "Заявка"=>$args['utheme'],
      "Имя"=>!empty($args['uname']) ? $args['uname'] : '',
      "Телефон: "=>!empty($args['uphone']) ? $args['uphone'] : '',
    ];
    $token = "";
    $chat_id = "";
    $txt="";
    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) {
      echo '<p class="success">Ваша заявка принята.</p>';
      return true;
    } else {
      echo '<p class="fail"><b>Ошибка.</b></p>';
    }
  } else {
    echo '<p class="fail">Ошибка. Заполните обязательные поля.</p>';
  }
} else {
  header ("Location: /");
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
Lesha, 2020-10-13
@KunaRaiden

strlen() - After receiving data, check the length of the string with the number;
if it is zero - an error ( well, or a message in the telegram that the number was not specified )
And if it is greater than zero (or 4) - take the last 4 digits using substr () and assign it to a variable (lastChars for example).
Then another request after $sendToTelegram
-

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

Well, at the end, checking $sendToTelegram_chat2 for true if ($sendToTelegram && $sendToTelegram_chat2)
PS I would replace fopen with curl

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question