🤖Free ChatGPT Line Bot with Horoscope, Music Broadcast, Google Image Search...
MIT License
Integrate ChatGPT Bot into Line, simply enter text in the input box to start interacting with ChatGPT.
Completely free ChatGPT Bot
Weekly horoscope information (real-time)
Scheduled push notifications for YT music channel
Online image search (real-time)
[!NOTE] If there're any new feature requirements, welcome to submit a PR or ISSUE
Python FastAPI
: Build ChatGPT response APIgpt4free
: Free to use OpenAI API
Line messaging API channel
: Connect to ChatGPT APIGithub
: Store the codereplit
: Free deployment of your own FastAPI
CronJob
: Free scheduled requests to prevent API interruptionsrender
, ngrok
: Other free deployment alternativesProvider
-> Click Create
Channel
-> Choose Create a Messaging API channel
Basic Settings
, there is a Channel Secret
-> Click Issue
, the generated value is LINE_CHANNEL_SECRET
(will be used later)Messaging API
, there is a Channel access token
-> Click Issue
, the generated value is LINE_CHANNEL_ACCESS_TOKEN
(will be used later)Star
to support the developerFork
to copy all the code to your own repositorySign Up
and log in with your Github
account and authorize it -> Click Skip
to skip the initial setupCreate
-> In the popup box, click the top right corner Import from Github
Connect GitHub to import your private repos.
-> Check Only select repositories
-> Choose ChatGPT-Line-Bot
Github URL
can choose the ChatGPT-Line-Bot
project -> Click Import from Github
.Environment Variable Setup
After completing the previous step Import
in Replit
, click Tools
at the bottom left of the project management page.
After clicking Got it
on the right side, you can add environment variables, you need to add:
LINE_CHANNEL_SECRET
[obtained from step 1]
LINE_CHANNEL_ACCESS_TOKEN
[obtained from step 1]
Start Execution
Run
at the topHello World
, and copy the URL in the upper right corner of the screenMessaging API
, paste the URL from above, and add /callback
to it, for example: https://ChatGPT-Line-Bot.jimmylin.repl.co/callback
Use webhook
Auto-reply messages
belowNote: If there are no requests within an hour, the program will be interrupted, so the next step is needed
CronJob Scheduled Requests
CREATE CRONJOB
in the upper right corner of the panelChatGPT-Line-Bot
in Title
, enter the URL from the previous step, for example: https://ChatGPT-Line-Bot.jimmylin.repl.co/
5 minutes
CREATE
Go back to Line Developer homepage and click Join friend guide
to scan the LINE Bot QR code:
Homepage -> Click your bot -> Click Add friends tool
-> Create friend action barcode (https://manager.line.biz/account//gainfriends)
Congratulations on creating your first LINE Bot! Try talking to it and see how it responds!
@chat
command at the beginning of your message. For example: @chat hi~
When your message includes a request for horoscope information, it triggers a web crawler to fetch the weekly horoscope:
Give me the Scorpio horoscope
, I want to know the Scorpio horoscope
, ...@chat Give me the Scorpio horoscope
, @chat I want to know the Scorpio horoscope
, ...When your message includes a request to search for images, it triggers a web crawler to fetch the desired image:
Search online for a picture of Lin Xiang smoking
, Find me a picture of Lin Xiang smoking online
, ...@chat Search online for a picture of Lin Xiang smoking
, @chat Find me a picture of Lin Xiang smoking online
, ...broadcast
API, we can send messages to every user at once using Line BotCreate the data ./data/favorite_videos.json
, you can refer to the author's data
(The data is created by fetching the author's favorite videos using the Youtube Data v3 API
, which is not specifically introduced here)
Use ./chatgpt_linebot/modules/youtube_recommend.py
to randomly select 3 songs and have GPT organize them
Add /recommend
route in ./chatgpt_linebot/urls.py
:
videos = recommend_videos() # Get 3 songs
if videos:
line_bot_api.broadcast(TextSendMessage(text=videos)) # Use broadcast to send messages to users
# Since broadcast cannot send push messages in groups, you can push messages to specified group ids through known group ids
# You can ignore the code below if you don't need to send messages to specific 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 the group_id
of a group, you can test it in the replit
console using print
:
elif event.source.type == 'group' and user_message.startswith('@chat'):
group_id = event.source.group_id
print(group_id) # Output group_id
memory.append(group_id, 'user', refine_message.replace('@chat', ''))
response = chat_completion(group_id, memory)
Now, when we access the /recommend
route, it will trigger the message broadcast, and all users and specified groups will receive the message
Next, we use cron-job.org again for scheduling. Set it to call this API every morning at 8:00 to achieve daily push notifications!
In addition, we can use the prompt
method to allow the Line Bot to answer in a personalized way. In ./chatgpt_linebot/prompts/template.py
, we can define a template
, for example:
Question: What did you have for breakfast today?
Bot Answer: Darling, did you wake up this morning? I've been waiting for you in bed, and I'm so hungry just thinking about your body. What should we have for breakfast today? How about a spicy omelette, as hot as your charming figure? 😏🍳
Since replit
no longer supports a free plan, the author provides the following two alternative solutions:
replit
, and will not be extensively discussed here. Although this solution is Serverless, it tends to be unstable.Download ngrok
for your operating system.
Add the path to ngrok.exe
to your environment variables.
In the Terminal, start FastAPI with:
pip install poetry
poetry shell
poetry install
$env:LINE_CHANNEL_SECRET="..."; $env:LINE_CHANNEL_ACCESS_TOKEN="..."; $env:SERPAPI_API_KEY="..."; poetry run python main.py
In the Terminal, run: ngrok config add-authtoken <token>
, where the token is from your personal account on the ngrok website.
In the Terminal, execute the following commands to start ngrok:
tskill /A ngrok
ngrok http 8080
Use the Forwarding URL as your Web URL.
Finally, remember to replace the obtained URL in the Webhook URL
section under Messaging API
in Line Developer.