{"id":3324,"date":"2025-12-03T18:25:34","date_gmt":"2025-12-03T10:25:34","guid":{"rendered":"https:\/\/blog.quantoyo.com\/?p=3324"},"modified":"2025-12-03T18:25:34","modified_gmt":"2025-12-03T10:25:34","slug":"line-bot-message-%e7%9a%84%e5%af%ab%e6%b3%95","status":"publish","type":"post","link":"https:\/\/blog.quantoyo.com\/?p=3324","title":{"rendered":"LINE BOT &#8211; Message \u7684\u5beb\u6cd5"},"content":{"rendered":"\n<p>\u6574\u7406\u4e00\u4e0b\u9019\u6b21\u7528\u5230\u7684 message \u7a2e\u985e\uff0c\u9019\u6a23\u5beb\u53ef\u4ee5\u8dd1&#8230;<\/p>\n\n\n\n<!--more-->\n\n\n\n<pre class=\"wp-block-code has-cyan-bluish-gray-background-color has-background has-small-font-size\"><code>&lt;?php\nrequire_once __DIR__ . '\/vendor\/autoload.php';\n\nuse GuzzleHttp\\Client;\nuse LINE\\Clients\\MessagingApi\\Configuration;\nuse LINE\\Clients\\MessagingApi\\Api\\MessagingApiApi;\nuse LINE\\Clients\\MessagingApi\\Model\\ReplyMessageRequest;\nuse LINE\\Clients\\MessagingApi\\Model\\TextMessage;\nuse LINE\\Clients\\MessagingApi\\Model\\ImageMessage;\nuse LINE\\Clients\\MessagingApi\\Model\\TemplateMessage;\nuse LINE\\Clients\\MessagingApi\\Model\\ButtonsTemplate;\nuse LINE\\Clients\\MessagingApi\\Model\\PostbackAction;\nuse LINE\\Clients\\MessagingApi\\Model\\URIAction;\nuse LINE\\Clients\\MessagingApi\\Model\\MessageAction;\nuse LINE\\Clients\\MessagingApi\\Model\\CarouselTemplate;\nuse LINE\\Clients\\MessagingApi\\Model\\CarouselColumn;\n\n\nTextMessage \/\/\u6700\u7c21\u55ae\u7684\u6587\u5b57\u8a0a\u606f\n\n $message&#91;] = new TextMessage(&#91;\n        'type' => 'text',\n        'text' => $msg\n    ]);\n\n\nImageMessage \/\/\u7528\u9019\u500b\u53ef\u4ee5\u50b3\u9001\u5716\u7247\n\n$message&#91;] = new ImageMessage(&#91;\n        'type' => 'image',\n        'originalContentUrl' => $imageUrl,\n        'previewImageUrl' => $imageUrl,\n    ]);\n\n\nURIAction \/\/\u9ede\u64ca\u53ef\u4ee5\u6253\u958b\u5167\u5efa\u700f\u89bd\u5668\u7684\u7db2\u9801\u9023\u7d50(\u6309\u9215)\n\n$url = \"https:\/\/blog.quantoyo.com\/ok.php\";\n    $action = new URIAction(&#91;\n        'type' => 'uri',\n        'label' => '\u9ede\u64ca\u6253\u958b\u9023\u7d50',\n        'uri' => $url\n    ]);\n\n    $message&#91;] = new TemplateMessage(&#91;\n        'type' => 'template',\n        'altText' => '\u8df3\u901a\u77e5\u7528\u7684\u6587\u5b57',\n        'template' => new ButtonsTemplate(&#91;\n            'type' => 'buttons',\n            'title' => '\u6a19\u984c\u6587\u5b57',\n            'text' => '\u8acb\u9ede\u64ca',\n            'actions' => &#91;$action] \/\/\u53ef\u4ee5\u653e\u597d\u5e7e\u500baction\n        ])\n    ]);\n\nMessageAction \/\/\u9ede\u64ca\u53ef\u4ee5\u5e6b\u4f7f\u7528\u8005\u50b3\u9001\u6587\u5b57\u7684\u6309\u9215\n\n    $action = new MessageAction(&#91;\n        'type' => 'message',\n        'label' => '\u9ede\u64ca\u6211\u767c\u9001ABC',\n        'text' => 'ABC' \/\/ \u9019\u662f\u6703\u767c\u9001\u7684\u6587\u5b57\n    ]);\n\n    $message&#91;] = new TemplateMessage(&#91;\n        'type' => 'template',\n        'altText' => '\u9019\u662f\u901a\u77e5\u7528\u7684\u6587\u5b57',\n        'template' => new ButtonsTemplate(&#91;\n            'type' => 'buttons',\n            'title' => '\u9019\u662f\u6a19\u984c',\n            'text' => '\u9019\u662f\u986f\u793a\u7684\u5167\u6587',\n            'actions' => &#91;$action]\n        ])\n    ]);\n\nPostbackAction \/\/ \u9ede\u64ca\u5f8c\uff0c\u6a5f\u5668\u4eba\u6703\u6536\u5230\u8a0a\u606f\uff0c\u4f46\u662f\u4e0d\u6703\u8b93\u4f7f\u7528\u8005\u5beb\u51fa\u4f86\u3002\n$tmpPostback = new PostbackAction(&#91;\n     'type' => 'postback',\n     'label' => '\u986f\u793a\u7684\u5b57',\n     'data' => '\u8981\u56de\u50b3\u7684\u5b57\u4e32'\n ]);\n $msgAry&#91;] = $tmpPostback;\n\n$message&#91;] = new TemplateMessage(&#91;\n      'type' => 'template',\n      'altText' => '\u8df3\u51fa\u901a\u77e5\u7684\u6587\u5b57', \/\/40\u500b\u5b57\u4ee5\u5167\n      'template' => new ButtonsTemplate(&#91;\n      'type' => 'buttons',\n      'title' => '\u9019\u662f\u6a19\u984c',\n      'text' => '\u9019\u662f\u5167\u6587',\n      'actions' => $msgAry\n   ])\n]); \n\nCarouselColumn \/\/\u5728\u756b\u9762\u4e0a\u505a\u4e00\u500b\u5f88\u591a\u9801\u7684\u5de6\u53f3\u6ed1\u52d5\u7684\u5716\u6587\u9078\u55ae\n\n$columnAry = array();\nfor($i = 0;$i &lt; count($plans); $i++){\n    $column = new CarouselColumn();\n    $column->setThumbnailImageUrl(\"\u5716\u7247\u7db2\u5740.png\");\n    $column->setImageBackgroundColor(\"#F0F0F0\");\n    $column->setTitle('\u7b2c'.$i.'\u500b\u7684\u6a19\u984c');\n    $column->setText('\u5167\u6587');\n    $column->setActions(&#91;\n        new PostbackAction(&#91;\n            'type' => 'postback',\n            'label' => \"\u6309\u9215\u4e0a\u9762\u7684\u6587\u5b57\",\n            'data' => '\u7b2c'.$i.'\u500b\u6309\u9215\u7684\u7b2c\u4e00\u500b\u9078\u9805\u8981\u56de\u50b3\u7684\u5b57'\n        ])\n        ,\n        new PostbackAction(&#91;\n            'type' => 'postback',\n            'label' => \"\u6309\u9215\u4e0a\u9762\u7684\u6587\u5b57\",\n            'data' => '\u7b2c'.$i.'\u500b\u6309\u9215\u7684\u7b2c\u4e8c\u500b\u9078\u9805\u8981\u56de\u50b3\u7684\u5b57'\n        ])\n    ]);\n    $columnAry&#91;] = $column;\n    if(count($columnAry) == 10){        \/\/Carousel \u6700\u591a10\u500b\n        break;\n    }\n}\n\/\/ \u5efa\u7acb Carousel Template\n$carousel = new CarouselTemplate();\n$carousel->settype('carousel');\n$carousel->setColumns($columnAry);\n\n$message&#91;] = new TemplateMessage(&#91;\n    'type' => 'template',\n    'altText' => '\u901a\u77e5\u7528\u7684\u5b57\u4e32',\n    'template' => $carousel\n]);<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u6574\u7406\u4e00\u4e0b\u9019\u6b21\u7528\u5230\u7684 message \u7a2e\u985e\uff0c\u9019\u6a23\u5beb\u53ef\u4ee5\u8dd1&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[632,637,636,630,71,626,635,623,76,633,628,631,629,634,627],"class_list":["post-3324","post","type-post","status-publish","format-standard","hentry","category-technology","tag-buttonstemplate","tag-carouselcolumn","tag-carouseltemplate","tag-imagemessage","tag-line-bot","tag-message","tag-messageaction","tag-messagingapiapi","tag-php","tag-postbackaction","tag-replymessagerequest","tag-templatemessage","tag-textmessage","tag-uriaction","tag-627"],"_links":{"self":[{"href":"https:\/\/blog.quantoyo.com\/index.php?rest_route=\/wp\/v2\/posts\/3324","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.quantoyo.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.quantoyo.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.quantoyo.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.quantoyo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3324"}],"version-history":[{"count":1,"href":"https:\/\/blog.quantoyo.com\/index.php?rest_route=\/wp\/v2\/posts\/3324\/revisions"}],"predecessor-version":[{"id":3325,"href":"https:\/\/blog.quantoyo.com\/index.php?rest_route=\/wp\/v2\/posts\/3324\/revisions\/3325"}],"wp:attachment":[{"href":"https:\/\/blog.quantoyo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3324"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.quantoyo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3324"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.quantoyo.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3324"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}