首页 文章

PhP中的Bot Telegram问题(键盘和内联)

提问于
浏览
0

使用:php连接webhook它正确如果我改为另一个php机器人它的工作原理:{“ok”:true,“result”:true,“description”:“Webhook已设置”}

我的机器人没有工作与Php我没有连接使用键盘内联的例子)这是source.php的代码我希望你们可以帮助我一些想法:S :(

<?php

define('api', 'https://api.telegram.org/bot'.token.'/');

$data = file_get_contents("php://input");
$update = json_decode($data,true);

$message = $update["message"];
$text = $message["text"];
$cid = $update["message"]["from"]["id"];
$from = $message["from"];
$username = $from["username"];
$nome = $from["first_name"];
$cognome = $form["last_name"];

$cbid = $update["callback_query"]["from"]["id"];
$cbdata = $update["callback_query"]["data"];

function callback($up){
    return $up[callback_query];
}

function apiRequest($metodo){
    $req = file_get_contents(api.$metodo);
    return $req;
}

function send($id,$text){
    if(strpos($text, "\n")){
        $text = urlencode($text);
    }
    return apiRequest("sendMessage?text=$text&parse_mode=HTML&chat_id=$id");
}

function keyboard($tasti, $text, $cd){
    $tasti2 = $tasti;

    $tasti3 = json_encode($tasti2);

        if(strpos($textm "\n")){
            $text = urlencode($text);
        }

 apiRequest("sendMessage?text=$text&parse_mode=Markdown&chat_id=$cd&reply_markup=$tasti3");
}

function inlinekeyboard($menud,$chat,$text){
    $menu = $menud;

        if(strpos($text, "\n")){
            $text = urlencode($text);
        }

        $d2 = array(
            "inline_keyboard" => $menu,
        );

        $d2 = json_encode($d2);

        return apiRequest("sendMessage?chat_id=$chat&parse_mode=Markdown&text=$text&reply_markup=$d2"); }

这是bot.php的代码

<?php

define('token', '<token>');

include 'source.php';

if($text == "/start"){
    send($cid, "bievenido al Bot inversion");
}

if($text == "tastiera"){
    $keyboard = [
        ["Inline", "Inline2"],
        ["Inline3", "Inline4"],
];
$key = array(
"resize_keyboard" => true,
"keyboard" => $keyboard,
);

    keyboard($key, "bievenido al Bot inversion", $cid);
}

if($text == "Inline"){
    $but = array(array(array("text" => "Bottone 1", "url" => "www.google.com"),),);
    inlineKeyboard($but, $cid , "Clicca questo pulsante per andare su google");
}

if($text == "Inline2"){
    $but = array(array(array("text" => "Bottone 1", "url" => "www.google.com"),array("text" => "Bottone 2", "url" => "www.facebook.it"),),);
    inlineKeyboard($but, $cid, "Clicca uno di questi due pulsanti \nper andare su google o su facebook!");
}

if($text == "Inline3"){
    $but[] = array(array("text" => "Bottone 1", "url" => "www.google.com"),);
    $but[] = array(array("text" => "Bottone 2", "url" => "www.facebook.it"),);
    inlineKeyboard($but, $cid, "Clicca uno di questi due pulsanti \nper andare su google o su facebook");   
}

if($text == "Inline4"){
    $but = array(array(array("text" => "Bottone 1", "callback_data" => "ciao1"),),);
    inlineKeyboard($but, $cid, "Clicca il bottone!");
}

if(callback($update)){
    if($cbdata == "ciao1"){
    send($cbid, "hai cliqueado el Bottone 1");
    }
}

1 回答

  • 0

    看看PHP的错误日志真的很有帮助 . 它会告诉你

    PHP解析错误:语法错误,[...]中的意外'“\ n”'(T_CONSTANT_ENCAPSED_STRING) . php xx行xx

    你在 if(strpos($textm "\n")) 错过了 , .

相关问题