【问题标题】:php telegram bot send photo don't runphp电报机器人发送照片不运行
【发布时间】:2016-06-24 17:03:19
【问题描述】:

我尝试制作电报机器人,但发送照片时出现问题。 这是我的代码:

我有这个功能:

function apiRequestImage($method, $parameters)
{

  if (!is_string($method)) {
    error_log("Method name must be a string\n");
    return false;
  }

  if (!$parameters) {
    $parameters = array();
  } else if (!is_array($parameters)) {
    error_log("Parameters must be an array\n");
    return false;
  }

  foreach ($parameters as $key => &$val) {
    // encoding to JSON array parameters, for example reply_markup
    if (!is_numeric($val) && !is_string($val)) {
      $val = json_encode($val);
    }
  }

  $urlpath = API_URL.$method.'?'.http_build_query($parameters);


  $handle = curl_init($urlpath);
  curl_setopt($handle, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($handle, CURLOPT_CONNECTTIMEOUT, 5);
  curl_setopt($handle, CURLOPT_TIMEOUT, 60);
}

这叫它

function processMessage($message) {
  // process incoming message
  $message_id = $message['message_id'];
  $chat_id = $message['chat']['id'];
  $img = curl_file_create('test.png','image/png');
  if (isset($message['text'])) {
    // incoming text message
    $text = $message['text'];

    if (strpos($text, "/start") === 0) {
      apiRequestJson("sendMessage", array('chat_id' => $chat_id, "text" => 'Hello', 'reply_markup' => array(
        'keyboard' => array(array('Hello', 'Hi','Bye')),
        'one_time_keyboard' => true,
        'resize_keyboard' => true)));
    } else if ($text === "Hello" || $text === "Hi") {
      apiRequest("sendMessage", array('chat_id' => $chat_id, "text" => 'Nice to meet you'));
    } else if ($text === "Bye") {
      apiRequestImage("sendPhoto", array('chat_id' => $chat_id, 'photo' => '@'.$img));
    }else if (strpos($text, "/stop") === 0) {
      // stop now
    } else {
      apiRequestWebhook("sendMessage", array('chat_id' => $chat_id, "reply_to_message_id" => $message_id, "text" => 'Cool'));
    }
  } else {
    apiRequest("sendMessage", array('chat_id' => $chat_id, "text" => 'I understand only text messages'));
  }
}

并调用所有

if (isset($update["message"])) {
  processMessage($update["message"]);
}

文字运行,但照片没有。 你能帮帮我吗?

其余代码:http://pastebin.com/BKPxe2KC

【问题讨论】:

    标签: php json api telegram-bot


    【解决方案1】:

    您的代码有多个部分。我建议逐个测试, 例如,您的functionkeyboard 部分,它似乎不起作用(电报语法错误),它可以阻止其他部分(如照片发送)。在我看来,你最好先处理一些事情(例如发送照片),当它做得最好时,然后继续添加键盘。(这是一个有效的键盘代码https://stackoverflow.com/a/37948952/6478645

    【讨论】:

      猜你喜欢
      • 2016-10-17
      • 2021-02-10
      • 1970-01-01
      • 2016-08-15
      • 1970-01-01
      • 2021-04-20
      • 1970-01-01
      • 2016-07-18
      • 2019-05-14
      相关资源
      最近更新 更多