├── demo ├── link.png ├── demo0.png ├── demo1.png ├── demo2.png ├── demo_3.png ├── link2.png ├── link3.png ├── link3a.png ├── link4.png └── demo_351.png ├── requirements.txt ├── vercel.json ├── README_jp.md ├── README_en.md ├── README.md └── main.py /demo/link.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pyfbsdk59/Flask-official-ChatGPT-TelegramBot-Vercel/HEAD/demo/link.png -------------------------------------------------------------------------------- /demo/demo0.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pyfbsdk59/Flask-official-ChatGPT-TelegramBot-Vercel/HEAD/demo/demo0.png -------------------------------------------------------------------------------- /demo/demo1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pyfbsdk59/Flask-official-ChatGPT-TelegramBot-Vercel/HEAD/demo/demo1.png -------------------------------------------------------------------------------- /demo/demo2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pyfbsdk59/Flask-official-ChatGPT-TelegramBot-Vercel/HEAD/demo/demo2.png -------------------------------------------------------------------------------- /demo/demo_3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pyfbsdk59/Flask-official-ChatGPT-TelegramBot-Vercel/HEAD/demo/demo_3.png -------------------------------------------------------------------------------- /demo/link2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pyfbsdk59/Flask-official-ChatGPT-TelegramBot-Vercel/HEAD/demo/link2.png -------------------------------------------------------------------------------- /demo/link3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pyfbsdk59/Flask-official-ChatGPT-TelegramBot-Vercel/HEAD/demo/link3.png -------------------------------------------------------------------------------- /demo/link3a.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pyfbsdk59/Flask-official-ChatGPT-TelegramBot-Vercel/HEAD/demo/link3a.png -------------------------------------------------------------------------------- /demo/link4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pyfbsdk59/Flask-official-ChatGPT-TelegramBot-Vercel/HEAD/demo/link4.png -------------------------------------------------------------------------------- /demo/demo_351.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pyfbsdk59/Flask-official-ChatGPT-TelegramBot-Vercel/HEAD/demo/demo_351.png -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | certifi 2 | chardet 3 | click 4 | flask 5 | future 6 | idna 7 | itsdangerous 8 | jinja2 9 | markupsafe 10 | python-telegram-bot==10.1.0 11 | requests 12 | urllib3 13 | werkzeug 14 | openai==0.27.0 15 | -------------------------------------------------------------------------------- /vercel.json: -------------------------------------------------------------------------------- 1 | { 2 | "builds": [{ 3 | "src": "main.py", 4 | "use": "@vercel/python", 5 | "config": { "maxLambdaSize": "15mb" } 6 | }], 7 | "routes": [ 8 | { 9 | "src": "/(.*)", 10 | "dest": "main.py" 11 | } 12 | ] 13 | } 14 | -------------------------------------------------------------------------------- /README_jp.md: -------------------------------------------------------------------------------- 1 | # Flask-ChatGPT-TelegramBot-Vercel 2 | # プラットフォーム Vercel 上に迅速に構築された Flask ChatGPT TelegramBot。 3 | 4 | 5 | ### [繁體中文](https://github.com/pyfbsdk59/Flask-ChatGPT-TelegramBot-Vercel/blob/main/README.md) 6 | ### [English](https://github.com/pyfbsdk59/Flask-ChatGPT-TelegramBot-Vercel/blob/main/README_en.md) 7 | 8 | 9 | #### 1. このプロジェクトは、以下のプロジェクトを参照しており、Flask を学習したばかりの人が Vercel に TelegramBot をデプロイするためのものです。 10 | 11 | https://github.com/howarder3/GPT-Linebot-python-flask-on-vercel

12 | https://github.com/zaoldyeck/telegram-innovation-chatbot/tree/basic 13 | 14 | #### 2. このプロジェクトは Vercel にデプロイされているため、コードは Docker のバージョンとは異なり、Flask フレームワークを使用し、webhook を設定する必要があります。 以下のWebhook設定のノウハウをご覧ください。 15 | 16 | https://zaoldyeck.medium.com/%E6%89%8B%E6%8A%8A%E6%89%8B%E6%95%99%E4%BD%A0%E6%80%8E%E9%BA%BC%E6%89%93%E9%80%A0-telegram-bot-a7b539c3402a 17 | 18 | #### 3. Vercel の環境変数で、OPENAI_API_KEY と TELEGRAM_BOT_TOKEN という 2 つの環境変数を設定する必要があります。 19 | 20 | #### 4. ブラウザを開き、以下の URL を入力し、Webhook を Vercel をデプロイした後の最後のステップとして設定してください。 フォーマットは次のように示されます。 https://api.telegram.org/bot{$token}/setWebhook?url={$webhook_url}。 21 | 22 | ##### 実際の例は次の例のようになります (直接コピーしないでください。自分のテレグラム トークンと Vercel プロジェクトの URL を使用してください)。: 23 | 24 | 25 | https://api.telegram.org/bot606248605:AAGv_TOJdNNMc_v3toHK_X6M-dev_1tG-JA/setWebhook?url=https://xxx.vercel.app/callback 26 | 27 | 28 | #### 5. 成功すると次のテキストが表示されます: 29 | 30 | { 31 | ok: true, 32 | result: true, 33 | description: "Webhook was set" 34 | } 35 | ------ 36 | ### テレグラム ボットの作成方法とトークンの取得方法については、こちらをご覧ください。. 37 | https://ithelp.ithome.com.tw/articles/10245264

38 | https://tcsky.cc/tips-01-telegram-chatbot/ 39 | 40 | -------------------------------------------------------------------------------- /README_en.md: -------------------------------------------------------------------------------- 1 | # Flask-ChatGPT-TelegramBot-Vercel 2 | # A Flask ChatGPT TelegramBot quickly built on the platform Vercel. 3 | 4 | 5 | ### [繁體中文](https://github.com/pyfbsdk59/Flask-ChatGPT-TelegramBot-Vercel/blob/main/README.md) 6 | ### [日本語](https://github.com/pyfbsdk59/Flask-ChatGPT-TelegramBot-Vercel/blob/main/README_jp.md) 7 | 8 | 9 | #### 0. Migrating to GPT3.5 model... 10 | 11 | 12 | 13 | #### 1. This project refers to the following predecessors' projects, and it is only for friends who have just learned Flask to deploy TelegramBot on Vercel: 14 | 15 | https://github.com/howarder3/GPT-Linebot-python-flask-on-vercel

16 | https://github.com/zaoldyeck/telegram-innovation-chatbot/tree/basic 17 | 18 | #### 2. Since this project is deployed on Vercel, the code is different from the version of Docker, and Flask framework must be used and webhook must be set. Please see knowhows of setting webhook below. 19 | 20 | https://zaoldyeck.medium.com/%E6%89%8B%E6%8A%8A%E6%89%8B%E6%95%99%E4%BD%A0%E6%80%8E%E9%BA%BC%E6%89%93%E9%80%A0-telegram-bot-a7b539c3402a 21 | 22 | #### 3. Two environment variables must be set in Vercel's Environment Variables, namely OPENAI_API_KEY and TELEGRAM_BOT_TOKEN. 23 | 24 | #### 4. Please open the browser, enter the following URL, and set the webhook as the last step after deploying Vercel. The format is shown as https://api.telegram.org/bot{$token}/setWebhook?url={$webhook_url}。 25 | 26 | ##### The actual example is like the following example (Don't copy it directly, please use your own telegram token and the URL of the Vercel project): 27 | 28 | 29 | https://api.telegram.org/bot606248605:AAGv_TOJdNNMc_v3toHK_X6M-dev_1tG-JA/setWebhook?url=https://xxx.vercel.app/callback 30 | 31 | 32 | #### 5. The following text will be shown after success: 33 | 34 | { 35 | ok: true, 36 | result: true, 37 | description: "Webhook was set" 38 | } 39 | ------ 40 | ### You can learn how to create a telegram bot and get the token here. 41 | https://ithelp.ithome.com.tw/articles/10245264

42 | https://tcsky.cc/tips-01-telegram-chatbot/ 43 | 44 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Flask-official-ChatGPT-TelegramBot-Vercel 2 | # 一個使用Flask框架和official ChatGPT/GPT3.5 turbo api創造TelegramBot,快速建置於平台Vercel。 3 | 4 |
5 | 6 |
7 | 8 | ## [TelegramBot Vercel GPT3版本部屬](https://github.com/pyfbsdk59/Flask-ChatGPT-TelegramBot-Vercel) 9 | 10 |
11 | 12 |
13 | 14 | ## [TelegramBot Render GPT3版本部屬](https://github.com/pyfbsdk59/Flask-ChatGPT-TelegramBot-Render) 15 | 16 |
17 | 18 |
19 | 20 | ## [LineBot Django Vercel GPT3版本部屬](https://github.com/pyfbsdk59/Django-ChatGPT-linebot-Vercel) 21 | 22 |
23 | 24 |
25 | 26 | ## [TelegramBot Golang Render GPT3版本部屬](https://github.com/pyfbsdk59/Golang-ChatGPT-TelegramBot-Render) 27 | 28 |
29 | 30 |
31 | 32 |
33 | 34 |
35 | 36 | ## [LineBot Golang Render GPT3版本部屬](https://github.com/pyfbsdk59/Golang-ChatGPT-linebot-Render) 37 | 38 |
39 | 40 |
41 | 42 | ### [English](https://github.com/pyfbsdk59/Flask-ChatGPT-TelegramBot-Vercel/blob/main/README_en.md) 43 | ### [日本語](https://github.com/pyfbsdk59/Flask-ChatGPT-TelegramBot-Vercel/blob/main/README_jp.md) 44 | 45 | 46 | 47 | 48 | #### 0. 程式猿影音教學參考。請支持且訂閱加按讚感謝他的辛勞。(GPT3版本) 49 | 50 | https://www.youtube.com/watch?v=eKKEa6NhCd0 51 | 52 | 53 |
54 | 55 |
56 | 57 | #### 1. 本專案參考了以下前輩的方案改成製作,只針對剛學習Flask的朋友來佈置TelegramBot在Vercel上: 58 | 59 | https://github.com/howarder3/GPT-Linebot-python-flask-on-vercel

60 | https://github.com/zaoldyeck/telegram-innovation-chatbot/tree/basic 61 | 62 | #### 2. 本專案因部屬在Vercel上,所以程式碼和Docker版本不同,也必須使用Flask和設定webhook。設定webhook請參考。 63 | 64 | https://zaoldyeck.medium.com/%E6%89%8B%E6%8A%8A%E6%89%8B%E6%95%99%E4%BD%A0%E6%80%8E%E9%BA%BC%E6%89%93%E9%80%A0-telegram-bot-a7b539c3402a 65 | 66 | 67 | #### 3. 註冊Vercel帳號後,import已經fork過來的本專案。 68 | 69 |
70 | 71 |
72 | 73 | #### 4. 必須在Vercel的Environment Variables設定兩個環境變數,分別是OPENAI_API_KEY和TELEGRAM_BOT_TOKEN。順便記下Vercel給的網址,可以自己縮短一些。 74 | 75 |
76 | 77 |
78 | 79 | #### 5. 打開瀏覽器,輸入以下網址,設定webhook為部屬完Vercel的最後步驟,格式為:https://api.telegram.org/bot{$token}/setWebhook?url={$webhook_url}。 80 | 81 | ##### 故實際範例就像以下範例(非直接複製使用,請改用自己的telegram token和Vercel專案的URL): 82 | 83 | 84 | https://api.telegram.org/bot606248605:AAGv_TOJdNNMc_v3toHK_X6M-dev_1tG-JA/setWebhook?url=https://xxx.vercel.app/callback 85 | 86 | 87 | #### 6. 成功後會顯示以下文字: 88 | 89 | { 90 | ok: true, 91 | result: true, 92 | description: "Webhook was set" 93 | } 94 | ------ 95 | ### 創建Telegram機器人和取得token,請參考: 96 | https://ithelp.ithome.com.tw/articles/10245264

97 | https://tcsky.cc/tips-01-telegram-chatbot/ 98 | 99 | -------------------------------------------------------------------------------- /main.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | import logging 4 | 5 | import telegram, os 6 | from flask import Flask, request 7 | from telegram.ext import Dispatcher, MessageHandler, Filters 8 | 9 | 10 | 11 | ################# 12 | import openai 13 | 14 | openai.api_key = os.getenv("OPENAI_API_KEY") 15 | 16 | chat_language = os.getenv("INIT_LANGUAGE", default = "zh") #amend here to change your preset language 17 | 18 | conversation = [] 19 | 20 | class ChatGPT: 21 | 22 | 23 | def __init__(self): 24 | 25 | self.messages = conversation 26 | self.model = os.getenv("OPENAI_MODEL", default = "gpt-3.5-turbo") 27 | 28 | 29 | 30 | def get_response(self, user_input): 31 | conversation.append({"role": "user", "content": user_input}) 32 | 33 | 34 | response = openai.ChatCompletion.create( 35 | model=self.model, 36 | messages = self.messages 37 | 38 | ) 39 | 40 | conversation.append({"role": "assistant", "content": response['choices'][0]['message']['content']}) 41 | 42 | print("AI回答內容:") 43 | print(response['choices'][0]['message']['content'].strip()) 44 | 45 | 46 | 47 | return response['choices'][0]['message']['content'].strip() 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | ##################### 57 | 58 | telegram_bot_token = str(os.getenv("TELEGRAM_BOT_TOKEN")) 59 | 60 | 61 | 62 | # Load data from config.ini file 63 | #config = configparser.ConfigParser() 64 | #config.read('config.ini') 65 | 66 | # Enable logging 67 | logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', 68 | level=logging.INFO) 69 | logger = logging.getLogger(__name__) 70 | 71 | # Initial Flask app 72 | app = Flask(__name__) 73 | 74 | # Initial bot by Telegram access token 75 | bot = telegram.Bot(token=telegram_bot_token) 76 | 77 | 78 | 79 | @app.route('/callback', methods=['POST']) 80 | def webhook_handler(): 81 | """Set route /hook with POST method will trigger this method.""" 82 | if request.method == "POST": 83 | update = telegram.Update.de_json(request.get_json(force=True), bot) 84 | 85 | # Update dispatcher process that handler to process this message 86 | dispatcher.process_update(update) 87 | return 'ok' 88 | 89 | 90 | def reply_handler(bot, update): 91 | """Reply message.""" 92 | #text = update.message.text 93 | #update.message.reply_text(text) 94 | chatgpt = ChatGPT() 95 | 96 | #update.message.text 人類的問題 the question humans asked 97 | ai_reply_response = chatgpt.get_response(update.message.text) #ChatGPT產生的回答 the answers that ChatGPT gave 98 | 99 | update.message.reply_text(ai_reply_response) #用AI的文字回傳 reply the text that AI made 100 | 101 | # New a dispatcher for bot 102 | dispatcher = Dispatcher(bot, None) 103 | 104 | # Add handler for handling message, there are many kinds of message. For this handler, it particular handle text 105 | # message. 106 | dispatcher.add_handler(MessageHandler(Filters.text, reply_handler)) 107 | 108 | if __name__ == "__main__": 109 | # Running server 110 | app.run(debug=True) --------------------------------------------------------------------------------