chatgpt-line-bot
chatgpt-line-bot copied to clipboard
🤖Free ChatGPT Line Bot with Horoscope, Music Broadcast, Google Image Search...
ChatGPT Line Bot
- English
- Traditional Chinese README.md
🤖Introduction
Integrate ChatGPT Bot into Line, simply enter text in the input box to start interacting with ChatGPT.
✨Features
-
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
🔨Tools
Python FastAPI: Build ChatGPT response APIgpt4free: Free to use OpenAI APILine messaging API channel: Connect to ChatGPT APIGithub: Store the codereplit: Free deployment of your own FastAPICronJob: Free scheduled requests to prevent API interruptions
🎈Installation Steps
Token Acquisition
- Obtain Line Token:
- Log in to Line Developer
- Create a bot:
- Create
Provider-> ClickCreate - Create
Channel-> ChooseCreate a Messaging API channel - Enter the required basic information
- After entering the information, under
Basic Settings, there is aChannel Secret-> ClickIssue, the generated value isLINE_CHANNEL_SECRET(will be used later) - Under
Messaging API, there is aChannel access token-> ClickIssue, the generated value isLINE_CHANNEL_ACCESS_TOKEN(will be used later)
- Create
Project Setup
- Fork the Github project:
- Register/Login to GitHub
- Go to ChatGPT-Line-Bot
- Click
Starto support the developer - Click
Forkto copy all the code to your own repository
- Deployment (Free Space):
- Go to replit
- Click
Sign Upand log in with yourGithubaccount and authorize it -> ClickSkipto skip the initial setup - On the main page in the middle, click
Create-> In the popup box, click the top right cornerImport from Github - If you haven't added your Github repository, click the link
Connect GitHub to import your private repos.-> CheckOnly select repositories-> ChooseChatGPT-Line-Bot - Go back to step 4, at this time the
Github URLcan choose theChatGPT-Line-Botproject -> ClickImport from Github.
Project Execution
-
Environment Variable Setup
-
After completing the previous step
ImportinReplit, clickToolsat the bottom left of the project management page. -
After clicking
Got iton the right side, you can add environment variables, you need to add:- Line Channel Secret:
- key:
LINE_CHANNEL_SECRET - value:
[obtained from step 1]
- key:
- Line Channel Access Token:
- key:
LINE_CHANNEL_ACCESS_TOKEN - value:
[obtained from step 1]
- key:
- Line Channel Secret:
-
-
Start Execution
- Click
Runat the top - After successful execution, the right side will display
Hello World, and copy the URL in the upper right corner of the screen - Go back to Line Developer, in
Messaging API, paste the URL from above, and add/callbackto it, for example:https://ChatGPT-Line-Bot.jimmylin.repl.co/callback - Turn on
Use webhook - Turn off
Auto-reply messagesbelow
-
Note: If there are no requests within an hour, the program will be interrupted, so the next step is needed
- Click
-
CronJob Scheduled Requests
- Register/Login to cron-job.org
- After logging in, click
CREATE CRONJOBin the upper right corner of the panel - Enter
ChatGPT-Line-BotinTitle, enter the URL from the previous step, for example:https://ChatGPT-Line-Bot.jimmylin.repl.co/ - Below, set it to run every
5 minutes - Click
CREATE
Connect Service and Line Bot
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/<yourBotId>/gainfriends)
Congratulations on creating your first LINE Bot! Try talking to it and see how it responds!
🎃Special Commands
| Command | Description |
|---|---|
@chat |
Enter @chat + message in the input box to call chatgpt in the Line group |
@chat Horoscope Fortune <Zodiac Sign> |
Enter @chat Horoscope Fortune Scorpio in the input box to get the weekly fortune of Scorpio (any zodiac sign can be used), this function is limited to Traditional Chinese commands |
@img <any input> |
Enter @img horoscope Lin Xiang smoking in the input box to perform an online image search. |
📢Broadcast Message - Daily Youtube Song Recommendation
- With the
broadcastAPI, we can send messages to every user at once using Line Bot - Here we want the Line Bot to randomly recommend 3 good Youtube songs every morning:
-
Create 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.pyto randomly select 3 songs and have GPT organize them -
Add
/recommendroute 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_idof a group, you can test it in thereplitconsole usingprint: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
/recommendroute, 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!
-
⚔Advanced - Personalized Bot
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? 😏🍳