【Javascript チャットロボット】Javascript タイピングゲーム チャット版 【保管庫】
個人的に作成した、JavaScript製のシンプルなチャットボットのソースコード保管庫(備忘録)です。
外部のAI(API)やデータベースは使用せず、HTMLファイル1つだけで動く非常に軽量なプログラムになっています。
あらかじめ登録したキーワードに対して自動で返答を返すだけの単純な仕組みです。後から自分でコードを見返したり、必要に応じて言葉の追加やカスタマイズがしやすいように、構造を極限までシンプルにしてここに保存しておきます。
パソコンのメモ帳にコードを貼り付け、「.html」の形式で保存すれば、ブラウザ上で直接動作を確認することができます。
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>🤖 SIMPLE CHAT BOT</title>
<style>
body { font-family: sans-serif; background: #f0f2f5; display: flex; justify-content: center; align-items: center; height: 100vh; margin: 0; }
.chat-container { width: 450px; height: 600px; background: white; border-radius: 12px; display: flex; flex-direction: column; overflow: hidden; box-shadow: 0 4px 15px rgba(0,0,0,0.1); }
.chat-header { background: #0084ff; color: white; padding: 15px; font-weight: bold; text-align: center; font-size: 16px; }
.chat-messages { flex: 1; padding: 20px; overflow-y: auto; display: flex; flex-direction: column; gap: 12px; background: #f9f9f9; }
.message { max-width: 80%; padding: 12px 16px; border-radius: 14px; font-size: 14px; line-height: 1.5; }
.message.bot { background: #e4e6eb; align-self: flex-start; color: black; }
.message.user { background: #0084ff; align-self: flex-end; color: white; }
.chat-input-area { display: flex; padding: 15px; background: #fff; border-top: 1px solid #ddd; }
.chat-input-area input { flex: 1; padding: 12px; background: #f0f2f5; color: #000; border: 1px solid #ccc; border-radius: 6px; outline: none; }
.chat-input-area button { background: #0084ff; color: white; border: none; padding: 0 20px; margin-left: 8px; border-radius: 6px; cursor: pointer; font-weight: bold; }
</style>
</head>
<body>
<div class="chat-container">
<div class="chat-header">💬 チャットボット</div>
<div class="chat-messages" id="chatMessages">
<div class="message bot">こんにちは!お疲れ様です。何かお話ししたいことがあれば気軽に入力してくださいね。</div>
</div>
<div class="chat-input-area">
<input type="text" id="userInput" placeholder="メッセージを入力してください..." onkeydown="if(event.key==='Enter') sendMessage()">
<button onclick="sendMessage()">送信</button>
</div>
</div>
<script>
const chatDatabase = [
{ key: ["こんにちは", "はじめまして", "ハロー"], replies: ["こんにちは!調子はどうですか?", "はじめまして!お話しできて嬉しいです。"] },
{ key: ["ありがとう", "助かった"], replies: ["どういたしまして!お役に立てて良かったです。😊", "そう言ってもらえると励みになります!"] },
{ key: ["ゲーム"], replies: ["ゲームは楽しいですよね。最近何かおすすめのタイトルはありますか?🎮", "休日にのんびりゲームをする時間は最高ですよね。"] },
{ key: ["飯", "めし", "ご飯", "食べる"], replies: ["今日のご飯は何にする予定ですか?美味しいものを食べると元気が出ますよね。🍛", "お腹がすいてきましたね。最近食べた美味しかったものの話などぜひ聞きたいです。"] },
{ key: ["疲れた", "眠い"], replies: ["今日もお疲れ様でした。本当に無理をなさらず、ゆっくり休んでくださいね。🍵", "一生懸命頑張った証拠ですね。温かいものでも飲んでリラックスしてください。"] }
];
const defaultReplies = [
"なるほど、そうなんですね!お話ししてくれてありがとうございます。",
"確かに、その通りですね。とても興味深いお話です。",
"ふむふむ、なるほど。他には何かありましたか?"
];
function sendMessage() {
const inputElement = document.getElementById("userInput");
const text = inputElement.value.trim();
if (text === "") return;
appendMessage(text, "user");
inputElement.value = "";
setTimeout(() => {
let replyText = "";
let matched = false;
for (let item of chatDatabase) {
if (item.key.some(k => text.includes(k))) {
const randomIndex = Math.floor(Math.random() * item.replies.length);
replyText = item.replies[randomIndex];
matched = true;
break;
}
}
if (!matched) {
const randomIndex = Math.floor(Math.random() * defaultReplies.length);
replyText = defaultReplies[randomIndex];
}
appendBotResponse(replyText);
}, 500);
}
function appendMessage(text, sender) {
const container = document.getElementById("chatMessages");
const msg = document.createElement("div");
msg.classList.add("message", sender);
msg.textContent = text;
container.appendChild(msg);
container.scrollTop = container.scrollHeight;
}
function appendBotResponse(text) {
appendMessage(text, "bot");
}
</script>
</body>
</html>チャット用(キーボード練習用)
https://toshiaki1.web.fc2.com/games/ChatAI/
Javascript タイピングゲーム(保管庫)