Integrate ChatGPT Bot into Line. Simply enter text in the input box to start interacting with ChatGPT.
-
Free ChatGPT Bot
-
Weekly horoscope information (real-time)
-
YouTube Music channel scheduled broadcasts
-
Online Image Search (real-time)
Note
If you have any feature requests, please submit a PR or ISSUE at any time.
Python FastAPI
:Create ChatGPT response APIgpt4free
:Free use of OpenAI APIzhipuai
:Free use of GPT APILine messaging API channel
:Connect to ChatGPT APIGitHub
:Code repositoryreplit/render/ngrok
:Free deployment of your own FastAPICronJob
:Free scheduled requests, for scheduled message broadcasting
Since g4f
depends on reverse engineering to call OpenAI's API, it may be unstable. Therefore, the author recommends using the Zhipu AI open platform as an alternative to free GPT API.
g4f
:Use reverse engineering to call OpenAI APIzhipuai
:Zhipu AI open platform provides free GPT API. Visit official website to register an account, no credit card or fees required. Add API key as shown below in Personal Center. Set this API key in the environment variable to use this GPT option.
- Obtain Line Tokens:
- Log in to Line Developer
- Create a bot:
- Create a
Provider
-> ClickCreate
- Create a
Channel
-> SelectCreate a Messaging API channel
- Fill in the required basic information
- After completing, go to
Basic Settings
-> ClickIssue
underChannel Secret
to generateLINE_CHANNEL_SECRET
(which will be used later). - Under
Messaging API
, clickIssue
to generateChannel access token
(which will be used later).
- Create a
-
Fork GitHub Project:
- Register/Login GitHub
- Go to ChatGPT-Line-Bot
- Click
Star
to support developers - Click
Fork
to copy all code to your repository
-
Start Python FastAPI Server:
$env:LINE_CHANNEL_SECRET="..."; $env:LINE_CHANNEL_ACCESS_TOKEN="..."; $env:SERPAPI_API_KEY="..."; $env:GPT_METHOD="..."; $env:GPT_API_KEY="..."; python main.py
GPT_METHOD
:Chooseg4f
orzhipuai
GPT_API_KEY
:If usingzhipuai
method, provide your API key
-
ngrok
:Use your local computer (or Google Colab) as a server to deploy API-
Download the
ngrok
version suitable for your operating system -
Add the
ngrok.exe
path to the system environment variables -
Execute:
ngrok config add-authtoken <token>
. Get the token from your personal ngrok dashboard. -
Execute:
ngrok http --url=<YOUR STATIC DOMAIN>.ngrok-free.app 8090
(if failed, please tryngrok http --hostname=<YOUR STATIC DOMAIN>.ngrok-free.app 8090
). Forward the URL as the webhook URL.
-
Finally, replace
http --url=<YOUR STATIC DOMAIN>.ngrok-free.app/callback
with the webhook URL in theMessaging API
area of the Line Developer console.
Go back to Line Developer Home, click Add Friend Guide
, scan the QR code to add Line Bot as a friend.
Home -> Select your bot -> Add Friend Tool -> Create friend action barcode (https://manager.line.biz/account//gainfriends)
Congratulations! You have created your first Line Bot. Try talking to it—it will respond to you!
- In one-on-one chats, any message will trigger a response.
- In group chats, use the
@chat
prefix to interact with the bot, such as,@chat hi~
.
When your message contains a horoscope information request, the web crawler will scrape the weekly horoscope:
- Private chat:
給我天蠍座星座
,我想知道天蠍座星座
, ... - Group chat:
@chat 給我天蠍座星座
,@chat 我想知道天蠍座星座
, ...
When your message contains a picture request, the web crawler will scrape the image:
- Private chat:
在線找到林翔抽煙的圖片
,給我在線林翔抽煙的圖片
, ... - Group chat:
@chat 在線找到林翔抽煙的圖片
,@chat 給我在線林翔抽煙的圖片
, ...
- Using the
broadcast
API, the Line Bot can send messages to all users at once. - This example demonstrates how the Line Bot can send 3 randomly selected YouTube songs every morning:
-
Create file
./data/favorite_videos.json
. Refer to the author's dataset.(The dataset is generated by using
YouTube Data v3 API
to crawl favorite videos. This guide does not involve the use of YouTube API.) -
Use
./ChatGPT_linebot/modules/youtube_recommend.py
to randomly select 3 songs, formatted by GPT. -
Add
/recommend
route in./ChatGPT_linebot/urls.py
:videos = recommend_videos() # Get 3 songs if videos: line_bot_api.broadcast(TextSendMessage(text=videos)) # Broadcast to users # Push message to known groups known_group_ids = [ 'C6d-xxxxxxxxxxxxxxxxxxxxxxxxxxxxx', 'Ccc-xxxxxxxxxxxxxxxxxxxxxxxxxxxxx', 'Cbb-xxxxxxxxxxxxxxxxxxxxxxxxxxxxx', ] for group_id in known_group_ids: line_bot_api.push_message(group_id, TextSendMessage(text=videos))
To get
group_id
, print in the console:elif event.source.type == 'group' and user_message.startswith('@chat'): group_id = event.source.group_id print(group_id) # Output group_id
-
Now, visit
/recommend
route to broadcast messages to all users and specified groups. -
Use cron-job.org to schedule a daily push at 8:00 AM:
- Register/Login cron-job.org
- Click the
CREATE CRONJOB
button at the top right - Title:
ChatGPT-Line-Bot
, URL: for example,https://ChatGPT-Line-Bot.jimmylin.repl.co/
- Set to run every
5 minutes
- Click
CREATE
-
-
Just like Daily YouTube Recommendation, just replace
/recommend
with/cwsChannel
. -
It can be set to run every
3 hours
in CronJob
You can use prompts to personalize the responses of the Line Bot. Define template
in ./ChatGPT_linebot/prompts/template.py
, for example:
User Input:我應該吃什麼早餐?
Bot Response:親愛的,你醒來了嗎?我一直在床上等你,想著你美麗的身材。我們應該吃什麼早餐?要不要來點辣的,比如熱蛋捲,配得上你火辣的魅力?😏🍳