Open25
#aider に指示を出してLINE Botを作ってみる #linedc
生成したコードはこちら
最初にaiderが動くようにしてく
インストール手順
OpenAI APIのAPIキーを発行する
- OpenAI
- OpenAI API
- Open API
があってややこしい
Modelはo1-preview
かclaude-3.5-sonnet
が良さそう
DevContainer化
aiderを起動
.aider*
を.gitignore
に追加するか聞かれるので許可する
node ➜ /workspace (main) $ aider --4o
────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
Add .aider* to .gitignore (recommended)? (Y)es/(N)o [Yes]: y
Added .aider* to .gitignore
Aider v0.59.1
Main model: gpt-4o-2024-08-06 with diff edit format
Weak model: gpt-4o-mini
Git repo: .git with 3 files
Repo-map: using 1024 tokens, auto refresh
VSCode terminal detected, pretty output has been disabled.
Use /help <question> for help, run "aider --help" to see cmd line args
────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
>
下記2ファイルにaiderのログが記録される様子
.aider.chat.history.md
# aider chat started at 2024-10-13 00:56:30
> Add .aider* to .gitignore (recommended)? (Y)es/(N)o [Yes]: y
> Added .aider* to .gitignore
> /usr/local/bin/aider --4o
> Aider v0.59.1
> Main model: gpt-4o-2024-08-06 with diff edit format
> Weak model: gpt-4o-mini
> Git repo: .git with 3 files
> Repo-map: using 1024 tokens, auto refresh
> VSCode terminal detected, pretty output has been disabled.
> Use /help <question> for help, run "aider --help" to see cmd line args
.aider.input.history
# 2024-10-13 00:56:34.528216
+y
.gitignore
は追加してくれるけどコミットされないので、手動でコミットしておく
node ➜ /workspace (main) $ git status
On branch main
Your branch is up to date with 'origin/main'.
Untracked files:
(use "git add <file>..." to include in what will be committed)
.gitignore
nothing added to commit but untracked files present (use "git add" to track)
node ➜ /workspace (main) $ git add .
node ➜ /workspace (main) $ git commit -m "feat: .gitignore"
[main 7f8ae04] feat: .gitignore
1 file changed, 1 insertion(+)
create mode 100644 .gitignore
node ➜ /workspace (main) $ git push
Enumerating objects: 4, done.
Counting objects: 100% (4/4), done.
Delta compression using up to 8 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 330 bytes | 330.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0 (from 0)
To ssh://github.com/mitsuoka0423/line-bot-aider.git
3772c84..7f8ae04 main -> main
node ➜ /workspace (main) $ git status
On branch main
Your branch is up to date with 'origin/main'.
nothing to commit, working tree clean
現状こんな感じ
aiderにコードを書いてもらう
aiderにお願いしてみる
オウム返しするLINE Botを書いて。Node.jsで
> オウム返しするLINE Botを書いて。Node.jsで
To create a LINE Bot that echoes messages using Node.js, we will need to create a new file for the bot's implementation. Here's a step-by-step outline of what we'll
do:
1 Create a new file, line-bot.js, to handle the LINE Bot logic.
2 Set up a basic Express server to receive webhook events from LINE.
3 Implement the echo functionality to reply with the same message received.
Let's start by creating the line-bot.js file:
line-bot.js
<<<<<<< SEARCH
=======
const express = require('express');
const line = require('@line/bot-sdk');
const config = {
channelAccessToken: 'YOUR_CHANNEL_ACCESS_TOKEN',
channelSecret: 'YOUR_CHANNEL_SECRET',
};
const app = express();
app.post('/webhook', line.middleware(config), (req, res) => {
Promise
.all(req.body.events.map(handleEvent))
.then((result) => res.json(result))
.catch((err) => {
console.error(err);
res.status(500).end();
});
});
const client = new line.Client(config);
function handleEvent(event) {
if (event.type !== 'message' || event.message.type !== 'text') {
return Promise.resolve(null);
}
const echo = { type: 'text', text: event.message.text };
return client.replyMessage(event.replyToken, echo);
}
app.listen(3000, () => {
console.log('LINE Bot is running on port 3000');
});
>>>>>>> REPLACE
Next, you will need to install the necessary dependencies:
npm install express @line/bot-sdk
Make sure to replace 'YOUR_CHANNEL_ACCESS_TOKEN' and 'YOUR_CHANNEL_SECRET' with your actual LINE Bot credentials. Once you've done that, you can start the server
with:
node line-bot.js
This will set up a basic LINE Bot that echoes back any text message it receives.
Tokens: 2.5k sent, 409 received. Cost: $0.01 message, $0.02 session.
line-bot.js
Create new file? (Y)es/(N)o [Yes]: y
Applied edit to line-bot.js
Commit 928a827 feat: Implement LINE Bot that echoes received messages using Node.js
npm install express @line/bot-sdk
Run shell command? (Y)es/(N)o/(S)kip all/(D)on't ask again [Yes]: y
Running npm install express @line/bot-sdk
added 75 packages in 3s
14 packages are looking for funding
run `npm fund` for details
Add command output to the chat? (Y)es/(N)o/(D)on't ask again [Yes]: y
node line-bot.js
Run shell command? (Y)es/(N)o/(S)kip all/(D)on't ask again [Yes]: y
Running node line-bot.js
LINE Bot is running on port 3000
-
line-bot.js
を生成 -
npm install express @line/bot-sdk
を実行 -
node line-bot.js
を実行
してくれた
ちなみに上記の生成に$0.03かかった様子
Tokens: 2.5k sent, 409 received. Cost: $0.01 message, $0.02 session.
生成されたline-bot.js
アクセストークンとチャネルシークレットを埋め込む必要がありますね
line-bot.js
const express = require('express');
const line = require('@line/bot-sdk');
const config = {
channelAccessToken: 'YOUR_CHANNEL_ACCESS_TOKEN',
channelSecret: 'YOUR_CHANNEL_SECRET',
};
const app = express();
app.post('/webhook', line.middleware(config), (req, res) => {
Promise
.all(req.body.events.map(handleEvent))
.then((result) => res.json(result))
.catch((err) => {
console.error(err);
res.status(500).end();
});
});
const client = new line.Client(config);
function handleEvent(event) {
if (event.type !== 'message' || event.message.type !== 'text') {
return Promise.resolve(null);
}
const echo = { type: 'text', text: event.message.text };
return client.replyMessage(event.replyToken, echo);
}
app.listen(3000, () => {
console.log('LINE Bot is running on port 3000');
});
トークンの発行方法も教えてくれる
> アクセストークンはどこで発行できる?
LINE Botのアクセストークンは、LINE Developersコンソールで発行できます。以下の手順で取得できます:
1 [LINE Developersコンソール](https://developers.line.biz/console/)にアクセスし、LINEアカウントでログインします。
2 プロバイダーを選択するか、新しいプロバイダーを作成します。
3 新しいチャネルを作成するか、既存のチャネルを選択します。
4 チャネルの設定ページに移動し、「Messaging API」タブを選択します。
5 「チャネルアクセストークン(長期)」のセクションで「発行」ボタンをクリックして、アクセストークンを生成します。
このアクセストークンをline-bot.jsのchannelAccessTokenに設定してください。
Tokens: 3.7k sent, 248 received. Cost: $0.01 message, $0.03 session.
──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
こちらはまだ取り込まれてない
アクセストークンをコードに書くと無邪気にコミットされそうなのでdotenv
を導入
> dotenvを導入して
To integrate dotenv into your project, we will:
1 Install the dotenv package.
2 Create a .env file to store your environment variables.
3 Update line-bot.js to load environment variables from the .env file.
Let's start by installing the dotenv package:
npm install dotenv
Next, create a .env file to store your LINE Bot credentials:
.env
<<<<<<< SEARCH
=======
CHANNEL_ACCESS_TOKEN=YOUR_CHANNEL_ACCESS_TOKEN
CHANNEL_SECRET=YOUR_CHANNEL_SECRET
>>>>>>> REPLACE
Finally, update line-bot.js to use dotenv to load these environment variables:
line-bot.js
<<<<<<< SEARCH
const express = require('express');
=======
require('dotenv').config();
const express = require('express');
>>>>>>> REPLACE
line-bot.js
<<<<<<< SEARCH
channelAccessToken: 'YOUR_CHANNEL_ACCESS_TOKEN',
channelSecret: 'YOUR_CHANNEL_SECRET',
=======
channelAccessToken: process.env.CHANNEL_ACCESS_TOKEN,
channelSecret: process.env.CHANNEL_SECRET,
>>>>>>> REPLACE
After making these changes, your application will load the LINE Bot credentials from the .env file. Make sure to replace 'YOUR_CHANNEL_ACCESS_TOKEN' and
'YOUR_CHANNEL_SECRET' in the .env file with your actual credentials.
Tokens: 3.9k sent, 279 received. Cost: $0.01 message, $0.04 session.
.env
Create new file? (Y)es/(N)o [Yes]:
Applied edit to .env
Applied edit to line-bot.js
Commit c2bf374 feat: Integrate dotenv for environment variable management in line-bot.js
npm install dotenv
Run shell command? (Y)es/(N)o/(D)on't ask again [Yes]:
Running npm install dotenv
added 1 package, and audited 77 packages in 522ms
15 packages are looking for funding
run `npm fund` for details
found 0 vulnerabilities
Add command output to the chat? (Y)es/(N)o/(D)on't ask again [Yes]:
You can use /undo to undo and discard each aider commit.
今気づいたけど.gitignore
は更新されないっぽい。
.env
とnode_modules
を追加しておきたい
> .env と node_modulesをgitignoreに追加して
Please add the .gitignore file to the chat so I can make the necessary changes.
Tokens: 3.6k sent, 19 received. Cost: $0.0091 message, $0.05 session.
Add .gitignore to the chat? (Y)es/(N)o/(D)on't ask again [Yes]:
Let's update the .gitignore file to include .env and node_modules:
.gitignore
<<<<<<< SEARCH
.aider*
=======
.aider*
.env
node_modules/
>>>>>>> REPLACE
Tokens: 3.6k sent, 45 received. Cost: $0.0095 message, $0.06 session.
Applied edit to .gitignore
Commit a09e373 chore: Add .env and node_modules to .gitignore
You can use /undo to undo and discard each aider commit.
指示出したら良い感じに変更してくれた
と、思ってたら、
> dotenvを導入して
の時点で.env
ファイルがコミットされてた
LINE Bot起動してみる
> line botを起動して
To start the LINE Bot, you can run the following command in your terminal:
node line-bot.js
This will start the server, and you should see the message "LINE Bot is running on port 3000" in your terminal, indicating that the bot is up and running.
Tokens: 3.7k sent, 62 received. Cost: $0.0099 message, $0.07 session.
node line-bot.js
Run shell command? (Y)es/(N)o/(D)on't ask again [Yes]:
Running node line-bot.js
LINE Bot is running on port 3000
オウム返しできた!