如何在PHP Bot Telegram

时间:2017-08-26 10:45:11

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

由于公司需要,我必须在php中编程...但我第一次使用php ...这是我第一次使用电报机器人:'( 在某种程度上,之前,当我运行命令/startdoWork时,一切正常......

但是现在我必须修改机器人,所有命令都隐藏在某个电报按钮后面...这里我是如何编辑我的php页面的:

if(strpos($text, "/start") === 0)
{
    $response = "Ciao $firstname, benvenuto!";

    $keyboard = [
        'inline_keyboard' => [
            [
                ['text' => 'forward me to groups']
            ]
        ]
    ];

    $encodedKeyboard = json_encode($keyboard);
    $parameters = 
        array(
            'chat_id' => $chatId, 
            'text' => $response, 
            'reply_markup' => $encodedKeyboard
        );
    $parameters["method"] = "sendMessage";
    echo json_encode($parameters);

}

使用BotFather我也运行了命令/setinline ...

所以我觉得我正在研究我parameters数组的问题..任何人都可以帮助我吗?

Ps。:(如果任何人都可以建议我也使用我喜欢的IDE ...我现在正在使用记事本++)

谢谢大家

2 个答案:

答案 0 :(得分:7)

首先,您不需要在botFather中使用/setinline命令。当您在正常聊天模式下使用inline_keyboard(自定义键盘)时,此命令用于“内联模式”。

您还需要在键盘数组中为每个按钮提供callback_data

$keyboard = [
    'inline_keyboard' => [
        [
            ['text' => 'forward me to groups', 'callback_data' => 'someString']
        ]
    ]
];
$encodedKeyboard = json_encode($keyboard);
$parameters = 
    array(
        'chat_id' => $chatId, 
        'text' => $response, 
        'reply_markup' => $encodedKeyboard
    );

send('sendMessage', $parameters); // function description Below

最后你需要通过curl发送它。这是我在我的代码中使用的函数:

function send($method, $data)
{
    $url = "https://api.telegram.org/bot<Bot-Token>". "/" . $method;

    if (!$curld = curl_init()) {
        exit;
    }
    curl_setopt($curld, CURLOPT_POST, true);
    curl_setopt($curld, CURLOPT_POSTFIELDS, $data);
    curl_setopt($curld, CURLOPT_URL, $url);
    curl_setopt($curld, CURLOPT_RETURNTRANSFER, true);
    $output = curl_exec($curld);
    curl_close($curld);
    return $output;
}

P.S。我个人使用PhpStorm,它很好;)

答案 1 :(得分:2)

我已经用https://github.com/irazasyed/telegram-bot-sdk管理了一些功能,如果您不想管理内联关键字的行,可以跳过foreach并使用$ inline_keyboard [] = $ keyword_data;。显示关键字

检查图片中的$ keyword_data 数组

$keyword_data array

 public function inlineKeyword($chat_id, $keyword_data, $msg = '') {
        if (empty($msg)) {
            $msg = "Select";
        }
        $inline_keyboard = array();
        $row = 0;
        $prev_value = '';
        foreach ($keyword_data as $value) {
            if (isset($prev_value['text']) && strlen($prev_value['text']) < 10 && strlen($value['text']) < 10) {
                $inline_keyboard[$row - 1][] = $value;
            } else {
                $inline_keyboard[$row][] = $value;
            }
            $prev_value = $value;
            $row++;
        }
//    $inline_keyboard[] = $keyword_data;
        $reply_markup = $this->telegram->replyKeyboardMarkup([
            'inline_keyboard' => $inline_keyboard,
            'resize_keyboard' => true
        ]);
        $response = $this->telegram->sendMessage([
            'text' => $msg,
            'reply_markup' => $reply_markup,
            'chat_id' => $chat_id
        ]);
    }

Function responce on telegram

相关问题