php电报机器人发送照片不运行

时间:2016-02-12 18:26:12

标签: php json api telegram-bot

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

我有这个功能:

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

1 个答案:

答案 0 :(得分:0)

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