我想在数据库中搜索内容时,Telegram bot错误

时间:2019-05-02 10:50:40

标签: php telegram-bot php-telegram-bot

我想为我的网站创建一个简单的Telegram机器人。 在该机器人中,用户将发送其api令牌,然后机器人将检查users表中是否存在api_token,并将电报用户ID保存在tel_id字段中。 但是现在当我发送正确的api_token时,机器人会说:未找到您的api令牌。 我的代码:

<?php
include '/det.php';
$telegram = json_decode(file_get_contents('php://input'),true);
$user_id = $telegram['message']['chat']['id'];
$text = $telegram['message']['text'];
$user_name = $telegram['message']['from']['first_name'];
$callback = $telegram['callback_query'];
$inline = $telegram['inline_query'];

// Start bot
if($text == "/start"){
    bot(
        'sendMessage', [
            'chat_id'=> $user_id,
            'text'=>"Hello\n Please send your api token.",
        ]
    );
}
else{
    // Create connection
    $conn = mysqli_connect($servername, $username, $password, $dbname);
    // Check connection
    if (!$conn) {
        bot(
            'sendMessage', [
            'chat_id'=>$user_id,
            'text'=> "Error",
            'parse_mode'=>'HTML'
            ]
        );  
    }
    $user = "SELECT name FROM users WHERE api_token='$text' ";
    $result = $conn->query($user);
    if($result->num_rows > 0){
        $user = "UPDATE users SET tel_id='$user_id' WHERE api_token='$text'";
        if (mysqli_query($conn, $user)) {
            bot(
                'sendMessage', [
                    'chat_id'=> $user_id,
                    'text'=> "Your Id saved.",
                    'parse_mode'=>'HTML'
                ]
            );  
        } 
        mysqli_close($conn);
    }
    else{
       bot(
            'sendMessage', [
                'chat_id'=>$user_id,
                'text' => "Your api token not found.",
                'parse_mode'=>'HTML'
            ]
        );  
    }
}
    function bot($method,$datas=[]){
     $url = "https://api.telegram.org/bot".API_KEY."/".$method; $ch = curl_init();
      curl_setopt($ch,CURLOPT_URL,$url); 
      curl_setopt($ch,CURLOPT_RETURNTRANSFER,true); 
      curl_setopt($ch,CURLOPT_POSTFIELDS,$datas); 
      $res = curl_exec($ch); 
      if(curl_error($ch)){
        var_dump(curl_error($ch)); 
      }else{ 
        return json_decode($res); 
      } 
    }

请注意,我将数据库详细信息放在det.php中 det.php代码:

<?php
define('API_KEY','Bot token'); 
$servername = 'localhost';
$username = 'database username';
$password = 'database password';
$dbname = 'database name';
?>  

0 个答案:

没有答案