PhP中的Bot电报问题(键盘和嵌入式)

时间:2018-09-09 05:19:24

标签: php bots telegram

使用:如果我更改为另一个php机器人,则带有连接web的php可以正确连接所有它: {“ ok”:true,“ result”:true,“ description”:“ Webhook已设置”}

我的机器人无法正常工作,使用键盘内联示例与Php im无关)这是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 :(得分:0)

看看PHP的错误日志确实很有帮助。 它会告诉你

  

PHP解析错误:语法错误,意外的“” \ n“”   (...)。php在xx行上的(T_CONSTANT_ENCAPSED_STRING)

您错过了,中的if(strpos($textm "\n"))