Sad*_*yan 3 php telegram telegram-bot
我用@botfather 创建了一个机器人,一切正常。现在我想将来自我的主机的命令设置为电报。我在我的根目录中创建了一个Bot.php。
bot.php
$string = json_decode(file_get_contents('php://input'));
    function objectToArray( $object )
    {
        if( !is_object( $object ) && !is_array( $object ) )
        {
            return $object;
        }
        if( is_object( $object ) )
        {
            $object = get_object_vars( $object );
        }
        return array_map( 'objectToArray', $object );
    }
    $result = objectToArray($string);
    $user_id = $result['message']['from']['id'];
    $text = $result['message']['text'];
    if($text == 'Hi')
    $text_reply = 'Hi';
if($text == 'Your name')
    $text_reply = 'jJoe';
    $token = '';
    $text_reply = 'Got you Buddy.';
    $url = 'https://api.telegram.org/bot'.tokenNumber.'/sendMessage?chat_id='.$user_id;
    $url .= '&text=' .$text_reply;
    $res = file_get_contents($url);  
现在当我浏览这个时:https://api.telegram.org/bot112186325:tokenNumber/setWebhook?url=https://partamsms.ir/bot.php  
我明白了: {"ok":true,"result":true,"description":"Webhook was set"}  
但是我无法在我的电报帐户中运行这些命令。
如何从我的服务器运行命令?
太感谢了
根据您的评论,您希望根据用户键入的消息做出不同的响应。因此,使用您的示例代码,您可以将其更改为如下所示:
// NOTE: you can pass 'true' as the second argument to decode as array
$result= json_decode(file_get_contents('php://input'), true);
error_log(print_r($result, 1), 3, '/path/to/logfile.log');
$user_id = $result['message']['from']['id'];
$text = $result['message']['text'];
// TODO: use something like strpos() or strcmp() for more flexibility
switch (true)
{
    case $text == '/hi':
        $text_reply = 'Hello';
        break;
    case $text == '/yourname':
        // TODO: use the getMe API call to get the bot information
        $text_reply = 'jJoe';
        break;
    default:
        $text_reply = 'not sure what you want?';
        break;
}
$token = '';
$url = 'https://api.telegram.org/bot'.tokenNumber.'/sendMessage?chat_id='.$user_id;
$url .= '&text=' .$text_reply;
$res = file_get_contents($url);  
因此,这几乎是对您已有内容的轻微重构...如果问题是您的Bot.php脚本未触发,则可能是因为该页面未公开。您指定给 Telegram 的 webhook 必须是可公开访问的 URL。我试图点击https://partamsms.ir/bot.php 但我无法访问它。
另一种方法是使用该getUpdates方法,并让脚本每 5 秒左右运行一次。
| 归档时间: | 
 | 
| 查看次数: | 6241 次 | 
| 最近记录: |