可以用这个接口“https://api.ownthink.com/bot”用JavaScript写一个对话机器人吗
时间: 2023-05-29 16:07:24 浏览: 63
是的,你可以使用这个接口来编写一个对话机器人。您需要使用JavaScript编写一个程序来调用API并处理响应。以下是一个简单的示例:
```javascript
// 定义API URL
const apiUrl = "https://api.ownthink.com/bot";
// 获取用户输入
const userInput = document.querySelector("#userInput");
// 获取回复消息的元素
const replyBox = document.querySelector("#replyBox");
// 监听用户输入的回车事件
userInput.addEventListener("keydown", (event) => {
if (event.keyCode === 13) {
// 按下回车键时,发送请求
const message = userInput.value;
const requestData = {
spoken: message,
appid: "your_appid_here", // 填入自己的appid
};
// 发送POST请求
fetch(apiUrl, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(requestData),
})
.then((response) => response.json())
.then((data) => {
// 处理响应
const replyMessage = data.data.info.text;
replyBox.innerHTML = replyMessage;
})
.catch((error) => console.error(error));
// 清空用户输入框
userInput.value = "";
}
});
```
注意:在上面的代码中,您需要将`your_appid_here`替换为您自己的appid。您可以在[OwnThink](https://www.ownthink.com/)网站上注册并获取自己的appid。