Загрузка...

Трабл с телеграмм ботом на пхп

Тема в разделе Backend создана пользователем Ricudosan 16 янв 2022. 284 просмотра

  1. Ricudosan
    Ricudosan Автор темы 16 янв 2022 1 27 авг 2019
    Написал бота, который связан с сайтом, функционал у него такой:
    Когда нажимаем старт, нам нужно авторизироваться, после правильного введённого кода, авторизация успешна.
    Далее, когда нам пишут комментарий на сайте он приходит нам в бота с текстом и callback data = /action_add
    Когда нажали на эту кнопку нам приходит введите ответ на комментарий, а затем после ввода мне нужно получить ответ, комментарий добавлен(вот это и не работает) кто знает как реализовать?
    PHP
    <?php
    ini_set('display_errors', 1);
    ini_set('display_startup_errors', 1);
    error_reporting(E_ALL);

    require_once "db.php";

    header("Cache-Control: must-revalidate");
    header("Pragma: no-cache");
    header("Expires: -1");

    $bottoken = "";
    $url = "https://api.telegram.org/bot$bottoken/sendMessage";

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

    file_put_contents(__DIR__ . '/message.txt', print_r($updateData, true));


    function security($string, $t = false)
    {
    $string = str_replace("\'", "", $string);
    if ($t != "m") $string = htmlspecialchars($string, ENT_QUOTES);
    $string = str_replace("--", "", $string);
    if ($t != "u") $string = str_replace("/", "", $string);
    //$string=str_replace(";","",$string);
    $string = str_replace(array("=", "+"), "", $string);
    $string = str_replace(" like ", " ", $string);
    $string = str_replace(" union ", " ", $string);
    $string = str_replace("%", "", $string);
    return $string;
    }

    function sql_security($string)
    {
    $string = strip_tags($string);
    return $string;
    }
    function curlx ($postfields,$url)
    {
    if (!$curld = curl_init()) {
    die();
    }
    curl_setopt($curld, CURLOPT_POST, true);
    curl_setopt($curld, CURLOPT_POSTFIELDS, $postfields);
    curl_setopt($curld, CURLOPT_URL,$url);
    curl_setopt($curld, CURLOPT_RETURNTRANSFER, true);
    $output = curl_exec($curld);
    curl_close ($curld);
    return $output;
    }



    //$sql = "SELECT id FROM users WHERE login_domain = '$uLd'";
    //$users_query = $mysqli->query("SELECT login, pass FROM users WHERE login='$login' AND pass='$pass'");
    //id пользователя


    // данные сообщения в зависимости от callback_query
    if (!empty($updateData['callback_query'])) {
    $message = $updateData['callback_query'];
    //$reply_markup = $message['message']['reply_markup']['inline_keyboard'][0][0]['callback_data'];
    $reply_markup = $message['data'];
    $text = trim($reply_markup);
    $typeData = true;

    } else {
    $message = $updateData;
    $text = trim($message['message']['text']);
    $typeData = false;
    }

    $user_id_1 = $message['from']['id'];
    $user_id = $message['message']['from']['id'];
    $user_name = $message['message']['from']['first_name'];
    //если сообщение не пусто
    if (!empty($message['message']['text'])) {
    $chat_id = $message['message']['chat']['id'];
    }




    // Check if callback is set
    /*
    if (isset($updateData['callback_query'])) {

    $postfields = array(
    'text' => 'Введите код авторизации : ',
    'chat_id' => $updateData['callback_query']['from']['id'],
    'parse_mode' => 'HTML');

    curlx ($postfields,$url);

    }
    */



    if (strpos($text, '/action_login') !== false) {
    $text = "/login";
    }


    $textClear = security(sql_security($text));

    $sql = "SELECT id, telega_id,telega_code FROM s_users WHERE telega_id = ".$chat_id." limit 1";
    $users_query = $mysqli->query($sql);
    $telega_id = false;
    $telega_code = false;
    $bd_user_id = false;
    if(!empty($users_query)) {
    foreach ($users_query as $row) {
    $telega_id = $row["telega_id"];
    $bd_user_id = $row["id"];
    $telega_code = $row["telega_code"];
    }
    }

    if(!empty($text)){
    if ((strpos($text, '/start') !== false)
    ||(strpos($text, '/login') !== false)) {
    if ($telega_id) {
    $postfields = array(
    'text' => 'Вы уже авторизованы',
    'chat_id' => $updateData['message']['from']['id'],
    'parse_mode' => 'HTML');
    curlx($postfields, $url);
    die();
    }
    }
    }


    switch ($text) {

    case '/login':
    $data = "Введите код авторизации";
    $keyboard = "";
    $postfields = array(
    'chat_id' => "$chat_id",
    'text' => "$data",
    'reply_markup' => $keyboard,
    'parse_mode' => 'HTML');
    echo curlx($postfields,$url);
    break;

    case '/action_add':

    $queryZ ="SELECT y.id, x.id,y.text as TEXT, x.name as NAME, o.telega_id as TELEGA_ID, o.id FROM s_comments y
    INNER JOIN s_products x ON y.object_id=x.id
    INNER JOIN s_users o ON x.user_id=o.id
    WHERE o.telega_id=".$updateData['callback_query']['from']['id']." order by y.id desc limit 1";

    $users_query = $mysqli->query($queryZ);
    $txt = false;
    if(!empty($users_query)) {
    foreach ($users_query as $row) {
    $txt = $row["TEXT"];
    }
    }

    $keyboard = array(
    "inline_keyboard" => array(array(array(
    "text" => "",
    "callback_data" => "/action_and",
    )))
    );

    $reply = "Введите ответ на комментарий:\n--------------\n$txt";
    $postfields = array(
    'chat_id' => "$user_id_1",
    'text' => "$reply",
    'reply_markup' => json_encode($keyboard),
    'parse_mode' => 'HTML');
    echo curlx($postfields,$url);
    break;


    // case '/action_and':
    // $keyboard = array(
    // "inline_keyboard" => array(array(array(
    // "text" => "ddd",
    // "callback_data" => "/action_add",
    // )))
    // );
    //
    // $reply = "Ответ коментарий добавлен";
    // $postfields = array(
    // 'chat_id' => "$chat_id",
    // 'text' => "$reply",
    // 'reply_markup' => json_encode($keyboard),
    // 'parse_mode' => 'HTML');
    // break;


    case '/start':
    $reply = "Вам необходимо авторизоваться";
    $keyboard = array(
    "inline_keyboard" => array(array(array(
    "text" => "Войти в кабинет",
    "callback_data"=>"/action_login",
    )))
    );


    $postfields = array(
    'chat_id' => "$chat_id",
    'text' => "$reply",
    'reply_markup' => json_encode($keyboard),
    'parse_mode' => 'HTML');
    echo curlx($postfields,$url);
    break;



    default:
    $text = security(sql_security($text));
    $sql = "SELECT id, telega_id, telega_code FROM s_users WHERE telega_code = '$text' limit 1";
    $users_query = $mysqli->query($sql);
    $u_id = false;
    $telega_id = false;
    $telega_code = false;
    if(!empty($users_query)) {
    foreach ($users_query as $row) {
    $u_id = $row["id"];
    $telega_id = $row["telega_id"];
    $telega_code = $row["telega_code"];
    }
    }

    if($text === $telega_code){
    $reply = "Добро пожаловать,$user_name!";

    $sql = "UPDATE s_users SET telega_id=".$chat_id." WHERE telega_code = '$text'";
    $mysqli->query($sql);


    $users_query = $mysqli->query($sql);

    $keyboard = "";
    $postfields = array(
    'chat_id' => "$chat_id",
    'text' => "$reply",
    'reply_markup' => $keyboard,
    'parse_mode' => 'HTML');
    echo curlx($postfields,$url);
    }else {

    if (strpos($telega_code, $bd_user_id) === false){

    $reply = "Код авторизации не действителен, попробуйте ещё раз $updateData[update_id]" ;
    $keyboard = array(
    "inline_keyboard" => array(array(array(
    "text" => "Войти в кабинет",
    "callback_data" => "/action_login",
    )))
    );
    $postfields = array(
    'chat_id' => "$chat_id",
    'text' => "$reply",
    'reply_markup' => json_encode($keyboard),
    'parse_mode' => 'HTML');

    echo curlx($postfields, $url);

    }



    }


    break;


    }
     
  2. Обменник
    Реализуй через сессии в базе данных
     
Загрузка...
Top