Integrate ChatGPT Bot into Line. Simply input text into the input box to start interacting with ChatGPT.
-
Completely free ChatGPT Bot
-
Weekly horoscope information (real-time)
-
Scheduled push for YouTube music channels
-
Online image search (real-time)
Note
If you have any feature requests, feel free to submit a PR or an ISSUE.
Python FastAPI
: Create the ChatGPT response APIgpt4free
: Use OpenAI API for freezhipuai
: Use GPT API for freeLine messaging API channel
: Connect ChatGPT APIGitHub
: Code repositoryreplit/render/ngrok
: Deploy your own FastAPI for freeCronJob
: Send scheduled requests for free, enabling scheduled push messages
Since g4f
relies on reverse engineering to call OpenAI's API, it can be unstable. Therefore, the author suggests an alternative solution using the Zhipu AI open platform for free GPT API access.
g4f
: Use reverse engineering to call OpenAI APIzhipuai
: Zhipu AI open platform offers free GPT API. Visit the official site to register an account without requiring any credit card or fees. Add an API key in the Personal Center as shown below. Set this API key in the environment variables to use this GPT option.
- Get 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 completion, go to
Basic Settings
-> UnderChannel Secret
, clickIssue
to generate theLINE_CHANNEL_SECRET
(used later). - Under
Messaging API
, clickIssue
to generate theChannel access token
(used later).
- Create a
- Fork the GitHub Project:
- Register/Log in to GitHub
- Navigate to ChatGPT-Line-Bot
- Click
Star
to support the developer - Click
Fork
to copy all the code to your repository
- Deployment:
ngrok
: Use a local computer (or Google Colab) as a server to deploy the API-
Download the appropriate version of
ngrok
for your OS -
Add the
ngrok.exe
path to your system's environment variables -
Launch FastAPI in the terminal:
$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 using thezhipuai
method, provide your API key
-
Run:
ngrok config add-authtoken <token>
. Obtain the token from your personal ngrok dashboard. -
Run:
ngrok http 8080
. The forwarding URL will be the webhook URL.
-
Finally, replace the webhook URL in the Line Developer console's Messaging API
section. (See Step 2 in Project Setup)
Go back to the Line Developer homepage, click Add Friend Guide
, and scan the QR code to add the Line Bot as a friend.
Homepage -> Select your bot -> Add friend tools -> Create Friend Action Barcode (https://manager.line.biz/account//gainfriends)
Congratulations! You've created your first Line Bot. Try talking to it—it will reply to you!
- In a one-on-one chat, any message will trigger a response.
- In group chats, use the
@chat
prefix to interact with the bot, e.g.,@chat hi~
.
When your message contains a request for horoscope information, a web crawler will fetch the weekly horoscope:
- Personal chat:
Give me the Scorpio horoscope
,I want to know the Scorpio horoscope
, ... - Group chat:
@chat Give me the Scorpio horoscope
,@chat I want to know the Scorpio horoscope
, ...
When your message contains a request for an image, a web crawler will fetch an image:
- Personal chat:
Find an image of Lin Xiang smoking online
,Give me an image of Lin Xiang smoking online
, ... - Group chat:
@chat Find an image of Lin Xiang smoking online
,@chat Give me an image of Lin Xiang smoking online
, ...
- Using the
broadcast
API, the Line Bot can push messages to all users at once. - This example demonstrates how the Line Bot can push 3 randomly selected YouTube songs every morning:
-
Create the file
./data/favorite_videos.json
. Refer to the author's dataset.(The dataset is generated using the
YouTube Data v3 API
to fetch favorite videos. This guide does not cover YouTube API usage.) -
Use
./chatgpt_linebot/modules/youtube_recommend.py
to randomly select 3 songs, formatted by GPT. -
Add a
/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 messages 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 retrieve group
group_id
, print it 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, hitting the
/recommend
route will broadcast messages to all users and specified groups. -
Use cron-job.org to schedule daily pushes at 8:00 AM:
- Register/Log in to cron-job.org
- Click
CREATE CRONJOB
in the top-right corner - Title:
ChatGPT-Line-Bot
, URL: e.g.,https://ChatGPT-Line-Bot.jimmylin.repl.co/
- Set to run every
5 minutes
- Click
CREATE
-
You can personalize the Line Bot's responses using prompts. Define template
in ./chatgpt_linebot/prompts/template.py
, for example:
User input: What should I have for breakfast?
Bot response: Darling, are you awake yet? I've been waiting in bed, thinking of your lovely figure. What should we have for breakfast? How about something spicy, like a hot omelet to match your fiery charm? 😏🍳