{"id":14044799,"url":"https://github.com/howarder3/chatgpt-linebot-using-python-flask-on-vercel","last_synced_at":"2025-07-27T19:30:46.215Z","repository":{"id":64766695,"uuid":"577722279","full_name":"howarder3/ChatGPT-Linebot-using-python-flask-on-vercel","owner":"howarder3","description":"GPT-Linebot using python flask for vercel","archived":false,"fork":false,"pushed_at":"2024-08-08T11:33:33.000Z","size":91,"stargazers_count":238,"open_issues_count":6,"forks_count":421,"subscribers_count":4,"default_branch":"master","last_synced_at":"2024-12-03T00:39:09.947Z","etag":null,"topics":["bot","chatgpt","flask","gpt","gpt-3","linebot","python"],"latest_commit_sha":null,"homepage":"https://www.wongwonggoods.com/portfolio/personal_project/gpt-linebot-python-flask-for-vercel/","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/howarder3.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-12-13T11:30:10.000Z","updated_at":"2024-12-01T18:07:02.000Z","dependencies_parsed_at":"2023-12-09T19:27:05.100Z","dependency_job_id":"905dcd00-52a1-43db-86c7-ebee93f3ad26","html_url":"https://github.com/howarder3/ChatGPT-Linebot-using-python-flask-on-vercel","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"purl":"pkg:github/howarder3/ChatGPT-Linebot-using-python-flask-on-vercel","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/howarder3%2FChatGPT-Linebot-using-python-flask-on-vercel","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/howarder3%2FChatGPT-Linebot-using-python-flask-on-vercel/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/howarder3%2FChatGPT-Linebot-using-python-flask-on-vercel/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/howarder3%2FChatGPT-Linebot-using-python-flask-on-vercel/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/howarder3","download_url":"https://codeload.github.com/howarder3/ChatGPT-Linebot-using-python-flask-on-vercel/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/howarder3%2FChatGPT-Linebot-using-python-flask-on-vercel/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":267413305,"owners_count":24083411,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-07-27T02:00:11.917Z","response_time":82,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["bot","chatgpt","flask","gpt","gpt-3","linebot","python"],"created_at":"2024-08-12T10:00:24.640Z","updated_at":"2025-07-27T19:30:45.871Z","avatar_url":"https://github.com/howarder3.png","language":"Python","funding_links":[],"categories":["Chatbots"],"sub_categories":[],"readme":"# ChatGPT-Linebot using python flask on vercel \n\n\u003e 作者註：本專案因為作者我的免費 API 額度已經用完了，然後我現在使用的是 `ChatGPT Plus`，\n\u003e \n\u003e 如果之後要我本人更新可能要等我哪天改用 ChatGPT API 了...\n\u003e \n\u003e 現在最新的版本感謝網友 @willismax 提供的 PR：https://github.com/howarder3/GPT-Linebot-python-flask-on-vercel/pull/17\n\u003e \n\u003e 但同樣的因為我沒有 ChatGPT API 的額度了... 所以也沒辦法測就直接 merge 了，\n\u003e\n\u003e 如果不能動再請大家幫忙發個 Issues/PR，感謝大大們🙏\n\n* last updated: 2023/10/9\n  - 更新說明 by @willismax，與修正的 [repo](https://github.com/willismax/GPT-Linebot-python-flask-on-vercel) (merged)\n    - vercel.json修正，改為第2版\n    - 修正單純安裝Flask==2.2.2 會與 Werkzeug 衝突的問題，在 requirements.txt 添加 Werkzeug==2.3.7 ([參考stack overflow](https://stackoverflow.com/questions/77213053/importerror-cannot-import-name-url-quote-from-werkzeug-urls))\n\n\u003e `本篇教學無經驗的新手也可學習，無須寫任何程式。`\n\u003e \n\u003e 無經驗預計 15 ~ 20 分鐘都可以完成。老手最快可能 5 分鐘就搞定\n\n* 這是使用 python flask 套件撰寫的 ChatGPT-Linebot\n* `不需寫 code`，只需去網頁設定一些內容，新手 15 分鐘內也能建立自己的 ChatGPT-Linebot\n* Why flask? 簡單好用，且支援 vercel\n* Why vercel? `免費`！！！免費額度就很夠一般使用，是 heroku 不再免費後的好選擇\n\n\u003e ~~註：ChatGPT 與 gpt 是同樣任務的模型，而目前透過 API 只能使用到 GPT-3 (本程式使用的方法)~~\n\u003e \n\u003e ~~而非 ChatGPT 使用的 GPT-3.5~~\n\u003e\n\u003e 已經改為 GPT-4，與最新的 ChatGPT 相同，只是改為使用 OpenAI API 的方式實現\n\n# 安裝步驟\n\n主要會有四個地方要去：(`這部份不看也沒關係，以下照著做就可以了！`)\n\n* 我的 github repo：透過 python 串接 openai 的 API，並透過 linebot sdk 提供簡單的訊息回復\n* openai：申請 OpenAI 的 API KEY\n* line developer：創建機器人\n* vercel：提供訊息回復，雖然是 serverless 但已經很符合我們的需求\n\n\n\n## step 1. 至 github fork 專案\n\n去我這個專案的 [github repo](https://github.com/howarder3/GPT-Linebot-python-flask-on-vercel \"github repo\")\n\n\n\n按下面的按鈕，\nfork 一份檔案到自己的帳號底下，等等我們會使用到\n\n\u003e `可以的話，旁邊的 Star 也幫我按一下，是對創作者最大的鼓勵！`\n\n[![](https://www.wongwonggoods.com/wp-content/uploads/2022/12/截圖-2022-12-13-下午7.27.12.png)](https://www.wongwonggoods.com/portfolio/personal_project/gpt-linebot-python-flask-for-vercel/attachment/%e6%88%aa%e5%9c%96-2022-12-13-%e4%b8%8b%e5%8d%887-27-12/)\n\n## step 2. 申請 OpenAI 的 API KEY\n\n可以直接去[這裡]( https://beta.openai.com/docs/quickstart/build-your-application \"這裡\")，一直往下拉，找到這個按鈕，並生成一個 API KEY\n\n\n\n[![](https://www.wongwonggoods.com/wp-content/uploads/2022/12/截圖-2022-12-13-下午6.11.22.png)](https://www.wongwonggoods.com/?attachment_id=8016)\n\n\n\u003e 請務必複製下來，這個 KEY 我們取名為 `OPENAI_API_KEY `\n\n## step 3. 去 line developer 建立一個新的機器人\n\n\u003e 這邊熟悉的人動作應該超快，可以略過，\n\u003e\n\u003e 以下教學是針對完全沒經驗的新手\n\n我們先到[ line developer 的首頁](https://developers.line.biz/zh-hant/ \" line developer 的首頁\")註冊一下，\n註冊完後，點選 Messaging API。\n\n[![](https://www.wongwonggoods.com/wp-content/uploads/2022/12/截圖-2022-12-13-下午6.32.33.png)](https://www.wongwonggoods.com/portfolio/personal_project/gpt-linebot-python-flask-for-vercel/attachment/%e6%88%aa%e5%9c%96-2022-12-13-%e4%b8%8b%e5%8d%886-32-33/)\n\n### step 3-1. 創建新的 channel\n\n第一次使用要創建一個新的 provider 與 channel，\n一個 provider 可以有很多 channel，\n「`而一個 channel 對應的就是一個 chatbot`」，\n這邊以下都照自己想要的名字跟事實填就好。\n\n\n[![](https://www.wongwonggoods.com/wp-content/uploads/2022/12/截圖-2022-12-13-下午6.36.49.png)](https://www.wongwonggoods.com/?attachment_id=8019)\n\n### step 3-2. 在 Basic Settings 的分頁，取得 LINE_CHANNEL_SECRET\n\n\n在 Basic Settings 的分頁，往下找到 channel secret\n\n\n[![](https://www.wongwonggoods.com/wp-content/uploads/2022/12/截圖-2022-12-13-下午6.40.36.png)](https://www.wongwonggoods.com/?attachment_id=8021)\n\n\n\n\u003e 請務必複製下來，這個 KEY 我們取名為 `LINE_CHANNEL_SECRET `\n\n\n### step 3-3. 在 Messaging API 的分頁，進行一些機器人初始設定 (並加機器人好友)\n\n再來我們去上方，選擇 Messaging API 的分頁，\n我們先關閉一些可能會吵的東西 (預設的自動回復之類的)，\n\n\n\u003e `這邊可以順便掃一下 QR code 或透過 line ID ，加機器人的 line 好友！　`\n\n\n我自己是設定如下：\n\n\n[![](https://www.wongwonggoods.com/wp-content/uploads/2022/12/截圖-2022-12-13-下午6.41.06.png)](https://www.wongwonggoods.com/?attachment_id=8022)\n\n\n* 允許加入群組要注意使用 openai 額度可能會超快\n* 「`自動回復訊息必關！！！`」，那是 line 的自動回復，不是我們要的\n* 歡迎訊息也可以關，這邊我是開著\n\n### step 3-4. 在 Messaging API 的分頁，取得 LINE_CHANNEL_ACCESS_TOKEN\n\n最後，在 Messaging API 的分頁的最下面，\n找到 channel access token，點選右邊發行，並把他記下來。\n\n\n\n[![](https://www.wongwonggoods.com/wp-content/uploads/2022/12/截圖-2022-12-13-下午6.41.14.png)](https://www.wongwonggoods.com/?attachment_id=8020)\n\n\n\n\u003e 請務必複製下來，這個 KEY 我們取名為 `LINE_CHANNEL_ACCESS_TOKEN `\n\u003e\n\u003e 請不要把這金鑰分享給別人，別人可能會拿去作壞事！！！\n\n\n`然後這邊網頁先不要關，等等還會用到！！！！`\n\n\n## step 4. 去 vercel 設定相關的環境變數，完成啟動機器人！\n\n\n### step 4-1. 新增 project\n\n去 [vercel 首頁](https://vercel.com/ \"vercel 首頁\")，add new project\n\n\n\nImport Git Repository，選擇你剛剛 fork 的專案 import\n\n[![](https://www.wongwonggoods.com/wp-content/uploads/2022/12/截圖-2022-12-13-下午7.39.19.png)](https://www.wongwonggoods.com/portfolio/personal_project/gpt-linebot-python-flask-for-vercel/attachment/%e6%88%aa%e5%9c%96-2022-12-13-%e4%b8%8b%e5%8d%887-39-19/)\n\n### step 4-2. 設定環境變數\n\n選擇「Environment Variables」，把我們剛剛紀錄的 OPENAI_API_KEY、LINE_CHANNEL_SECRET、LINE_CHANNEL_ACCESS_TOKEN 都設定至環境變數，\n按下 Deploy 等待一下就完成囉！\n\n\n\n\n[![](https://www.wongwonggoods.com/wp-content/uploads/2022/12/截圖-2022-12-13-下午7.46.13.png)](https://www.wongwonggoods.com/portfolio/personal_project/gpt-linebot-python-flask-for-vercel/attachment/%e6%88%aa%e5%9c%96-2022-12-13-%e4%b8%8b%e5%8d%887-46-13/)\n\n\n* 完成後我們可以看到會有以下的三個環境變數\n\n\n[![](https://www.wongwonggoods.com/wp-content/uploads/2022/12/截圖-2022-12-13-下午7.47.23.png)](https://www.wongwonggoods.com/portfolio/personal_project/gpt-linebot-python-flask-for-vercel/attachment/%e6%88%aa%e5%9c%96-2022-12-13-%e4%b8%8b%e5%8d%887-47-23/)\n\n###  step 4-3. deploy 完成後，可以簡單確認是否有成功\n\n去部屬完成的 vercel 頁面，紅框處可以拿到我們要用的網址，\n\n`把這個網址複製下來，等等要用`\n\n這個網址我們也可以點開，應該會出現我們在程式預先寫好的 Hello, World! (應該是一個只有 Hello, World! 的網頁)\n我們可以藉此確定程式有正常的被 Deploy\n\n\n[![](https://www.wongwonggoods.com/wp-content/uploads/2022/12/截圖-2022-12-14-上午1.25.48.png)](https://www.wongwonggoods.com/portfolio/personal_project/gpt-linebot-python-flask-for-vercel/attachment/%e6%88%aa%e5%9c%96-2022-12-14-%e4%b8%8a%e5%8d%881-25-48/)\n\n\n## step 5. 設定 webhook\n\n\n回到 line developer 的 Messaging API 分頁，\n將剛剛 step 4-3. 的網址填入，並在後面加上 「/webhook」，例如下圖\n可以用 Verify 看看有沒有問題，通常應該會是寫「Success」\n\n* 「`記得開啟下面的使用 Use webhook `」\n\n\n\n[![](https://www.wongwonggoods.com/wp-content/uploads/2022/12/截圖-2022-12-13-下午9.29.23.png)](https://www.wongwonggoods.com/portfolio/personal_project/gpt-linebot-python-flask-for-vercel/attachment/%e6%88%aa%e5%9c%96-2022-12-13-%e4%b8%8b%e5%8d%889-29-23/)\n\n\n\n# 完成圖範例\n\n\n[![](https://www.wongwonggoods.com/wp-content/uploads/2022/12/截圖-2022-12-12-下午11.24.29.png)](https://www.wongwonggoods.com/?attachment_id=8017) \n\n\n[![](https://www.wongwonggoods.com/wp-content/uploads/2022/12/截圖-2022-12-12-下午11.21.45.png)](https://www.wongwonggoods.com/?attachment_id=8018)\n\n\n\n\n# 此 linebot 的其他一些內建功能 \n\n## 機器人「說話開關」\n\n這個本來是我除錯用的，因為有時候回復一些怪東西會很吵，\n意外得到好評，所以這個功能就被保留下來\n\n* 輸入「說話」：機器人開啟說話模式，預設是開啟的\n* 輸入「閉嘴」：機器人暫停說話模式 (`但一段時間會自動再起動`)，閉嘴後將不會對任何對話有反應。輸入「說話」可再次開啟對話。\n\n## 其他環境參數功能\n\n參考自 [memochou1993/gpt-ai-assistant](https://github.com/memochou1993/gpt-ai-assistant?fbclid=IwAR25uqLdKoDKEQd591fSjyM2sDJJR3Xb-VgcXDIFV_7i3RMWWv2oiyG26RQ) 的作法，下列參數也可藉由設定 vercel 的環境變數來作調控。\n\n|環境變數名稱             |預設值           |說明 |\n|------------------------|--------------- |-----|\n| OPENAI_MODEL            | text-davinci-003| 請參考 OpenAI 對 [model](https://beta.openai.com/docs/api-reference/completions/create#completions/create-model) 的敘述|\n| OPENAI_TEMPERATURE      | 0               | 請參考 OpenAI 對 [temperature](https://beta.openai.com/docs/api-reference/completions/create#completions/create-temperature) 的敘述|\n| OPENAI_FREQUENCY_PENALTY| 0               | 請參考 OpenAI 對 [frequency_penalty](https://beta.openai.com/docs/api-reference/completions/create#completions/create-frequency_penalty) 的敘述|\n| OPENAI_PRESENCE_PENALTY | 0.6             | 請參考 OpenAI 對 [presence_penalty](https://beta.openai.com/docs/api-reference/completions/create#completions/create-presence_penalty) 的敘述|\n| OPENAI_MAX_TOKENS       | 240             | 請參考 OpenAI 對 [max_tokens](https://beta.openai.com/docs/api-reference/completions/create#completions/create-max_tokens) 的敘述|\n| MSG_LIST_LIMIT          | 20              | prompt 參數往回參照的句數|\n| INIT_LANGUAGE           | zh              | 決定初始語言，可設置為 \"zh\" 或 \"en\"|\n\n\n\n# TODO List \u0026 Future Work\n\n\n\u003e 目前基本功能都已經有了，然後我比較忙可能沒空一直更新QQ\n\u003e\n\u003e `還有很多可以優化的地方，歡迎提供 PR！`\n\n- [x] (已調整完成) 回復文字感覺不是很順 (可能需要研究一下 API 使用方法)\n- [x] (已調整完成) 記憶功能 \n...\n\n\u003e 目前算法是「`紀錄使用者與 AI 的前20句對話`」，嘗試推論出下一句話應該要說什麼。以達成延續話題的效果。\n\n\n# 靈感來源\n* 本文同步更新至我的個人網站：[【Side Project】(全圖文教學) 用 Python flask 實作類似 ChatGPT 的 Linebot，並部屬至 vercel 上](https://www.wongwonggoods.com/portfolio/personal_project/gpt-linebot-python-flask-for-vercel/)\n* 感謝 [memochou1993/gpt-ai-assistant](https://github.com/memochou1993/gpt-ai-assistant?fbclid=IwAR25uqLdKoDKEQd591fSjyM2sDJJR3Xb-VgcXDIFV_7i3RMWWv2oiyG26RQ) 提供的 node.js 版本串接 vercel 示範，讓我有了想把 python linebot 也串進 vercel 的靈感，(目前感覺下來，免費又好用(?))\n* 感謝 [Lanznx/HealthLineBot](https://github.com/Lanznx/HealthLineBot) 給了一個很好的 python Django 範例，然而我不會 Django XD，vercel 官方文件好像也沒有提到這部份，總之後來就改成了 flask 版本，也符合 linebot 推薦的範例。\n\n\n\n# 參考資料\n\n* Line 官方提供的 python flask 製作 linebot 的 sample code [line/line-bot-sdk-python](https://github.com/line/line-bot-sdk-python)\n* Vercel 官方提供的 python runtime Flask 範例 [Deploy an example with Flask](https://vercel.com/docs/concepts/functions/serverless-functions/runtimes/python#python-version)\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhowarder3%2Fchatgpt-linebot-using-python-flask-on-vercel","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhowarder3%2Fchatgpt-linebot-using-python-flask-on-vercel","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhowarder3%2Fchatgpt-linebot-using-python-flask-on-vercel/lists"}