E
E
Evgeny2021-12-16 23:48:13
PHP
Evgeny, 2021-12-16 23:48:13

How to get chat and message data in telegram bot with different webhooks?

I have a telegram bot to which two webhooks are attached - with a telegram api and another service. The problem is that I use webhooks in different files that do not depend on each other in any way, but the webhook on another service does not receive data from the telegram (for example, chat_id and message_id). It can work if you manually substitute all the ids and other values

. I don’t understand how you can take data from the telegram and not use specific ids when sending messages

.

<?php

// Делается запрос
$method = 'setWebhook';
$url = 'https://api.telegram.org/bot' . BOT_TOKEN . '/' . $method;
$options = [
    'url' => 'https://incrediblebot.ru/notifications_bot.php'
];

// То, что возвращается из запроса
$response = file_get_contents($url . '?' . http_build_query($options));
var_dump($response);

// Функция отправки сообщений
function sendRequest($method, $post = '') {
    $ch = curl_init('https://api.telegram.org/bot' . BOT_TOKEN . '/' . $method);

if ($post) {
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
}

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($ch);
curl_close($ch);
}

$data = json_decode(file_get_contents('php://input'), JSON_OBJECT_AS_ARRAY);

$chat_id = $data['message']['chat']['id']; // ID чата
$chat_type = $data['message']['chat']['type']; // Тип чата
$message = $data['message']['text']; // Текст сообщения
$user_id = $data['message']['from']['id']; // ID юзера
$username = $data['message']['from']['username']; // Юзернейм
$message_id = $data['message']['message_id']; // ID сообщения

$callback_query = $data->callback_query;
$callback_query_id = $callback_query->id;
$callback_data = $callback_query->data;
    
switch ($callback_data) {
    case 'start':
        $job_id_get = job_id_get($job_id);
        exec('curl --request POST --header "PRIVATE-TOKEN: <token>" "https://example.com"');
    $post = [
        'chat_id' => $chat_id,
        'text' => $callback_data . ' ' . $job_id_get['job_id']
    ];
    sendRequest('sendMessage', $post);
        break;
}


The code of the file with the webhook of another service:

$input = file_get_contents("php://input");
$json = json_decode($input);

function sendRequest($method, $post = '')
{
    $ch = curl_init('https://api.telegram.org/bot <TOKEN>' . '/' . $method);

if ($post) {
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
}

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$json = curl_exec($ch);
curl_close($ch);
}

if ($json) {

    $ref = $json->object_attributes->ref;
    $project_name = $json->project->name;
    $name = $json->user->name;
    $username = $json->user->username;
    $stages = $json->object_attributes->stages[0];

if ($status == 'manual') {
        job_id($job_id);
        $post = [
            'chat_id' => 123,
            'text' => ' <b>Webhook caught manual event!</b>',
            'parse_mode' => 'html',
            $start_button = [],
            'reply_markup' => inline_keyboard($start_button)
        ];
        sendRequest('sendMessage', $post);
    }
}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question