Q&A

How to send a link in text message - Messaging API

前提・実現したいこと

I want to send a url in text message. Messing API

発生している問題・エラーメッセージ

該当のソースコード

function send_line_notification2($line_id = "", $text_message = "") {

	$fields = array(
		'to' => $line_id,
		'messages' => array(array('type' => 'text', 'text' => 'This is text message'), array('type' => 'uri', 'label' => 'Open Zoom Meeting', 'uri'=>'https://accent-language.com'))
	);
	$url = "https://api.line.me/v2/bot/message/push";
	$fields = json_encode($fields);
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL, $url);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
	curl_setopt($ch, CURLOPT_POST, 1);
	$line_access_token = env('LINE_ACCES_TOKEN');
	$headers = array();
	$headers[] = "Content-Type: application/json";
	$headers[] = "Authorization: Bearer {$line_access_token}";
	curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
	$result = curl_exec($ch);
	curl_close($ch);
	
	//echo '<pre>';print_r($result);exit;
}

試したこと

I tried abobe code. but line message is not get send.

補足情報(FW/ツールのバージョンなど)

  • 0
  • 1
  • 202
  • twitter facebook

You can use flex message for sending url text. https://developers.line.biz/ja/reference/messaging-api/#button

So you should change like following.

$messages = json_decode(<<< EOS
[{
  "type": "flex",
  "altText": "test",
  "contents": {
    "type": "bubble",
    "body": {
      "type": "box",
      "layout": "vertical",
      "contents": [
        {
          "type": "text",
          "text": "hello"
        },
        {
          "type": "button",
          "action": {
            "type": "uri",
            "label": "Open Zoom Meeting",
            "uri": "https://accent-language.com"
          },
          "style": "primary",
          "color": "#0000ff"
        }
      ]
    }
  }
}]
EOS
);

curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode([
       'to' => $line_id,
       'messages' => $messages,
]));
  • 0
本当によろしいですか? question.vm