{"id":495,"date":"2025-06-02T13:57:16","date_gmt":"2025-06-04T06:47:15","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=495"},"modified":"2025-06-04T13:57:16","modified_gmt":"2025-06-04T06:47:15","slug":"line-bot-%e6%95%99%e5%ad%b8","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/line\/line-bot-%e6%95%99%e5%ad%b8\/","title":{"rendered":"[2025 \u6700\u65b0] Python Line Bot \u6a5f\u5668\u4eba\u6559\u5b78 &#8211; \u7bc4\u4f8b\u8207\u5be6\u4f5c"},"content":{"rendered":"<p>&#8220;`html<\/p>\n<h1>Python Line Bot \u6559\u5b78<\/h1>\n<p>\u5728\u9019\u7bc7\u6587\u7ae0\u4e2d\uff0c\u6211\u5011\u5c07\u5b78\u7fd2\u5982\u4f55\u4f7f\u7528 Python \u5efa\u7acb\u4e00\u500b\u7c21\u55ae\u7684 Line Bot\u3002\u9019\u500b\u6559\u5b78\u9069\u5408\u6240\u6709\u60f3\u8981\u4e86\u89e3\u5982\u4f55\u88fd\u4f5c\u804a\u5929\u6a5f\u5668\u4eba\u7684\u670b\u53cb\uff0c\u7121\u8ad6\u4f60\u662f\u521d\u5b78\u8005\u9084\u662f\u6709\u4e00\u5b9a\u57fa\u790e\u7684\u958b\u767c\u8005\u3002<\/p>\n<h2>\u4ec0\u9ebc\u662f Line Bot?<\/h2>\n<p>Line Bot \u662f\u4e00\u7a2e\u81ea\u52d5\u5316\u7684\u804a\u5929\u6a5f\u5668\u4eba\uff0c\u80fd\u5920\u5728 Line \u804a\u5929\u5e73\u53f0\u4e0a\u8207\u7528\u6236\u4e92\u52d5\u3002\u900f\u904e Line Bot\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u70ba\u7528\u6236\u63d0\u4f9b\u5404\u7a2e\u81ea\u52d5\u56de\u8986\u670d\u52d9\uff0c\u4e26\u8a2d\u8a08\u5be6\u7528\u529f\u80fd\uff0c\u8b93\u7528\u6236\u80fd\u5920\u8f15\u9b06\u7372\u53d6\u6240\u9700\u8cc7\u8a0a\u3002<\/p>\n<h2>\u5efa\u7acb Line Bot<\/h2>\n<p>\u9996\u5148\uff0c\u4f60\u9700\u8981\u5230 <a href=\"https:\/\/developers.line.biz\/zh-hant\/\" target=\"_blank\" rel=\"noopener\">Line Developers<\/a> \u8a3b\u518a\u4e26\u5275\u5efa\u4e00\u500b\u804a\u5929\u983b\u9053\u3002\u4ee5\u4e0b\u662f\u8a73\u7d30\u6b65\u9a5f\uff1a<\/p>\n<h3>1. \u5275\u5efa\u4e00\u500b\u804a\u5929\u983b\u9053<\/h3>\n<p><img decoding=\"async\" src=\"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2020\/11\/line-bot-pythin-1.png\" alt=\"Line Bot\u8a2d\u5b9a\" title=\"\"> <\/p>\n<p>\u9032\u5165 Line Developers\uff0c\u9078\u64c7\u300c\u5275\u5efa\u65b0\u983b\u9053\u300d\u4e26\u6309\u7167\u6307\u793a\u64cd\u4f5c\u3002<\/p>\n<h3>2. \u8f38\u5165\u767c\u884c\u8005\u540d\u7a31<\/h3>\n<p><img decoding=\"async\" src=\"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2020\/11\/line-bot-pythin-2.png\" alt=\"Line Bot\u600e\u9ebc\" title=\"\"> <\/p>\n<p>\u8f38\u5165\u4f60\u7684\u767c\u884c\u8005\u540d\u7a31\uff0c\u9019\u5c07\u986f\u793a\u5728\u7528\u6236\u7684\u804a\u5929\u5217\u8868\u4e2d\u3002<\/p>\n<h3>3. \u9ede\u64ca Create a Messaging API channel<\/h3>\n<p><img decoding=\"async\" src=\"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2020\/11\/line-bot-pythin-3.png\" alt=\"\u4ec0\u9ebc\u4e8bLine Bot\" title=\"\"> <\/p>\n<p>\u9078\u64c7 Messaging API\uff0c\u7136\u5f8c\u9ede\u64ca\u300c\u5275\u5efa\u300d\u3002<\/p>\n<h3>4. \u586b\u5beb\u983b\u9053\u57fa\u672c\u8cc7\u6599<\/h3>\n<p><img decoding=\"async\" src=\"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2020\/11\/line-bot-pythin-4.png\" alt=\"Line Bot\u67b6\u8a2d\" title=\"\"><\/p>\n<p>\u6839\u64da\u9700\u6c42\u586b\u5beb\u5269\u9918\u7684\u983b\u9053\u57fa\u672c\u8cc7\u6599\u3002<\/p>\n<h3>5. \u9ede\u64ca\u5275\u5efa<\/h3>\n<p><img decoding=\"async\" src=\"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2020\/11\/line-bot-pythin-5.png\" alt=\"Line Bot\u81ea\u52d5\u56de\u8986\" title=\"\"><\/p>\n<h3>6. \u9ede\u64ca Messaging API<\/h3>\n<p><img decoding=\"async\" src=\"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2020\/11\/line-bot-pythin-7.png\" alt=\"Line Bot\u514d\u8cbb\" title=\"\"><\/p>\n<h3>7. \u9ede\u64ca Auto-reply messages<\/h3>\n<p><img decoding=\"async\" src=\"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2020\/11\/line-bot-pythin-9.png\" alt=\"Line Bot\u61c9\u7528\" title=\"\"><\/p>\n<h3>8. \u505c\u7528\u6b61\u8fce\u8a0a\u606f\u8207\u81ea\u52d5\u56de\u61c9\u8a0a\u606f<\/h3>\n<p><img decoding=\"async\" src=\"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2020\/11\/line-bot-pythin-10.png\" alt=\"Line Bot\u6559\u5b78\" title=\"\"><\/p>\n<h2>Line Bot Python \u7a0b\u5f0f\u7bc4\u4f8b<\/h2>\n<h3>1. \u5275\u5efa config.ini<\/h3>\n<pre><code class=\"language-python line-numbers\">[line-bot]\nchannel_access_token = \u4f60\u7684token\nchannel_secret = \u4f60\u7684\u91d1\u9470\n<\/code><\/pre>\n<h3>2. \u53d6\u5f97 Channel access token<\/h3>\n<p>\u5728 Messaging API \u4e2d\uff0c\u4f60\u53ef\u4ee5\u627e\u5230 Channel access token\uff0c\u9ede\u9078 issue \u5f8c\u6703\u986f\u793a\u4f60\u7684 token\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2020\/11\/wp_editor_md_9a5a0ed75551c3811b73b876727e822b.jpg\" alt=\"line\u81ea\u52d5\u56de\u8986\" title=\"\"><\/p>\n<h3>3. \u53d6\u5f97 Channel secret<\/h3>\n<p>\u5728 Basic settings \u4e2d\uff0c\u4f60\u53ef\u4ee5\u627e\u5230 Channel secret\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2020\/11\/wp_editor_md_0c59cb78b624b83eec0536067651d78b.jpg\" alt=\"Line\u6a5f\u5668\u4eba\u6559\u5b78\" title=\"\"><\/p>\n<h3>4. \u5efa\u7acb app.py<\/h3>\n<p>\u4ee5\u4e0b\u662f\u4e00\u500b\u7c21\u55ae\u7684 Line Bot \u7a0b\u5f0f\u78bc\u7bc4\u4f8b\uff1a<\/p>\n<pre><code class=\"language-python line-numbers\">from __future__ import unicode_literals\nimport os\nfrom flask import Flask, request, abort\nfrom linebot import LineBotApi, WebhookHandler\nfrom linebot.exceptions import InvalidSignatureError\nfrom linebot.models import MessageEvent, TextMessage, TextSendMessage\nimport configparser\nimport random\n\napp = Flask(__name__)\n\n# LINE \u804a\u5929\u6a5f\u5668\u4eba\u7684\u57fa\u672c\u8cc7\u6599\nconfig = configparser.ConfigParser()\nconfig.read('config.ini')\n\nline_bot_api = LineBotApi(config.get('line-bot', 'channel_access_token'))\nhandler = WebhookHandler(config.get('line-bot', 'channel_secret'))\n\n@app.route(\"\/callback\", methods=['POST'])\ndef callback():\n    signature = request.headers['X-Line-Signature']\n    body = request.get_data(as_text=True)\n    \n    try:\n        handler.handle(body, signature)\n    except InvalidSignatureError:\n        abort(400)\n    return 'OK'\n\n@handler.add(MessageEvent, message=TextMessage)\ndef handle_message(event):\n    line_bot_api.reply_message(\n        event.reply_token,\n        TextSendMessage(text=event.message.text)\n    )\n\nif __name__ == \"__main__\":\n    app.run()\n<\/code><\/pre>\n<p>\u5982\u679c\u7f3a\u5c11\u4e00\u4e9b SDK\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u6307\u4ee4\u5b89\u88dd\uff1a<\/p>\n<pre><code class=\"language-sh line-numbers\">$ pip3 install flask line-bot-sdk<\/code><\/pre>\n<h2>Ngrok \u600e\u9ebc\u8a2d\u7f6e<\/h2>\n<p>Ngrok \u662f\u4e00\u500b\u65b9\u4fbf\u7684\u5de5\u5177\uff0c\u80fd\u5c07\u672c\u5730\u4f3a\u670d\u5668\u6620\u5c04\u5230\u4e92\u806f\u7db2\u4e0a\u3002\u9019\u6a23\u4f60\u5c31\u53ef\u4ee5\u5728\u672c\u5730\u74b0\u5883\u6e2c\u8a66\u4f60\u7684 Line Bot\u3002\u4ee5\u4e0b\u662f\u8a2d\u7f6e\u65b9\u6cd5\uff1a<\/p>\n<h3>1. Ngrok \u5b89\u88dd\u6b65\u9a5f<\/h3>\n<p><img decoding=\"async\" src=\"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2020\/11\/line-bot-pythin-14.png\" alt=\"Line Bot Python\" title=\"\"><\/p>\n<h3>2. \u89e3\u58d3\u7e2e Ngrok.zip<\/h3>\n<pre><code class=\"language-sh line-numbers\">unzip ngrok.zip<\/code><\/pre>\n<h3>3. \u8a2d\u7f6e Ngrok token<\/h3>\n<pre><code class=\"language-sh line-numbers\">ngrok authtoken \u4f60\u7684token<\/code><\/pre>\n<h3>4. \u8a2d\u7f6e Ngrok port<\/h3>\n<pre><code class=\"language-sh line-numbers\">ngrok http 5000<\/code><\/pre>\n<h3>5. \u66f4\u63db Webhook URL<\/h3>\n<p>\u5728 Line Developers \u4e2d\uff0c\u5c07 Webhook URL \u66f4\u6539\u70ba Ngrok \u63d0\u4f9b\u7684\u7db2\u5740\u52a0\u4e0a &#8220;\/callback&#8221;\u3002<\/p>\n<h3>6. \u958b\u555f\u52a0\u5165\u7fa4\u7d44\u6b0a\u9650<\/h3>\n<p>\u5728 Messaging API \u4e2d\uff0c\u5141\u8a31\u6a5f\u5668\u4eba\u52a0\u5165\u7fa4\u7d44\u804a\u5929\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2020\/11\/wp_editor_md_373f38b3b1632c70c8ec94ba47429790.jpg\" alt=\"Line Bot PHP\" title=\"\"><\/p>\n<h2>\u5e38\u898b\u554f\u984c\u89e3\u7b54<\/h2>\n<h3>Q1: Line Bot \u9700\u8981\u54ea\u4e9b\u6b0a\u9650\uff1f<\/h3>\n<p>Line Bot \u9700\u8981 Messaging API \u7684\u6b0a\u9650\uff0c\u4e26\u4e14\u53ef\u4ee5\u9078\u64c7\u662f\u5426\u5141\u8a31\u52a0\u5165\u7fa4\u7d44\u804a\u5929\u3002<\/p>\n<h3>Q2: \u5982\u4f55\u6e2c\u8a66\u6211\u7684 Line Bot\uff1f<\/h3>\n<p>\u4f60\u53ef\u4ee5\u4f7f\u7528 Ngrok \u4f86\u5c07\u672c\u5730\u4f3a\u670d\u5668\u516c\u958b\uff0c\u7136\u5f8c\u900f\u904e Line \u61c9\u7528\u7a0b\u5f0f\u8207\u4f60\u7684\u6a5f\u5668\u4eba\u4e92\u52d5\u3002<\/p>\n<h3>Q3: \u5982\u679c\u9047\u5230\u932f\u8aa4\u8a72\u600e\u9ebc\u8fa6\uff1f<\/h3>\n<p>\u6aa2\u67e5\u4f60\u7684 token \u548c secret \u662f\u5426\u6b63\u78ba\uff0c\u4e26\u78ba\u4fdd Ngrok \u6b63\u5728\u904b\u884c\u3002<\/p>\n<p>\u53c3\u8003\u8cc7\u6599\u53ef\u4ee5\u53c3\u8003 [Python Line Bot \u6559\u5b78](https:\/\/www.learncodewithmike.com\/2020\/06\/python-line-bot.html) \u4ee5\u7372\u53d6\u66f4\u591a\u8cc7\u8a0a\u3002<\/p>\n<p>&#8220;`<br \/>\n&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>## \u7a0b\u5f0f<br \/>\n\u5275\u5efa\u4e00\u500b config.ini<br \/>\n### config.ini<br \/>\n&#8220;` python<br \/>\n[line-bot]<br \/>\nchannel_access_token = \u4f60\u7684token<br \/>\nchannel_secret =  \u4f60\u7684\u91d1\u9470<br \/>\n&#8220;`<\/p>\n","protected":false},"author":1,"featured_media":507,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"aside","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[10,18],"tags":[11,106],"class_list":["post-495","post","type-post","status-publish","format-aside","has-post-thumbnail","hentry","category-line","category-python","tag-line-bot","tag-106","post_format-post-format-aside"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2020\/11\/line-bot-pythin-1.png","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-7Z","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/495","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/comments?post=495"}],"version-history":[{"count":31,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/495\/revisions"}],"predecessor-version":[{"id":13083,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/495\/revisions\/13083"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media\/507"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=495"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=495"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=495"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}