【问题标题】:Telegram bot Send message电报机器人发送消息
【发布时间】:2017-03-19 07:27:54
【问题描述】:

我正在使用 irazasyed.github.io Telegram SDK 发送消息 这是我向机器人发送消息时的代码,没有任何反应,也没有收到回复 问题出在哪里 ?

MY_BOT_TOKEN 被替换

<?php

require 'vendor/autoload.php';

use Telegram\Bot\Api;

$telegram = new Api('MY_BOT_TOKEN');

$update = json_decode( file_get_contents( 'php://input' ) ); 
$chat_id = $update->getMessage()->getChat()->getId();

$response = $telegram->sendMessage([
    'chat_id' => $chat_id; 
    'text' => 'Hello World'
]);
$response->getMessageId();

?>

【问题讨论】:

  • 我相当肯定函数json_decode 不会返回具有这些方法的对象。如果我错了,请纠正我,但您应该会收到一条错误消息,指出 $update 对象没有这些方法。有关 json_decode 函数应返回什么的更多信息,请查看该函数的 manual page

标签: php bots telegram


【解决方案1】:

需要做一些改变:

$update = json_decode( file_get_contents( 'php://input' ) , true); 
$chat_id = $update->getMessage->getChat->getId();

【讨论】:

    猜你喜欢
    • 2017-09-01
    • 2016-03-11
    • 1970-01-01
    • 2019-10-30
    • 2019-12-31
    • 2017-05-11
    • 2021-01-29
    • 2020-08-25
    • 2020-12-10
    相关资源
    最近更新 更多