How to create serverless telegram chatbot using google app script and CHATGPT API from OpenAI.
- Google Gmail account ( where your android playstore account is )
- OpenAI account , see https://beta.openai.com/account/api-keys
- Telegram chatbot token, see https://t.me/BotFather
[1] Goto https://script.google.com/home , click "New Project"
[2] Write the scripts, copy paste from below template url :
https://raw.githubusercontent.com/WingsMaker/chatgptbot/main/chatgpt_gas.txt
[3] Rename the project
Click on the "Untitled project" to rename the project as something else. Example, "My ChatGPT bot".
[4] Deploy as google web app In the Google App Script project, go to "Publish" in the top navigation bar. Under "Deploy as web app", select "Deploy". This will open a pop-up window. Your web app URL will be listed in the "Current web app URL" field.
Click on the "Deploy - New Deployment"
Click "Select type - Web App"
Fill in the form and click "Deploy". ( for more see https://www.youtube.com/watch?v=-AlstV1PAaA )
Copy the web app url to the clipboard for later use.
[5] Update the values of webappUrl in the script. Change the value of statement, paste the url address into here.
var webappUrl = "paste_here";
[6] Update the values of token in the script. Find your Telegram chatbot token by logging into the BotFather in Telegram, selecting your bot, and clicking the "API Token" button. ( see https://www.youtube.com/watch?v=aNmRNjME6mE ) Change the value of statement, paste the bot token into here.
var token = "replace_here";
[7] Update the values of apiKey in the script. The api secret key for Chat GPT is located in the My Apps page of your Chat GPT dashboard. ( for more see https://www.youtube.com/watch?v=DFmmiYlbgX0 ) Change the value of statement, paste the api secret key into here.
var apiKey = "replace_here";
[8] Save the script and REDO the deployment process in step 4 since the code has been changed to fill in the webappUrl, token, apiKey.
[9] Run the "setWebhook" function for only once to make sure actual telegram bot able to callback this google web app.
Your telegram bot is ready !