0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

しょうもないBOTに会話AIを積む

Posted at

僕、マイクラサーバーを運営しているのですが、放置に使っているBOTに
Chaplus APIというAIサービスを使ってみたのでまとめてみました

僕はmineflayerというJavaScriptでマイクラBOTを作れるやつでBOT本体を実装してまてます。

APIをたたく部分を作る

npm i axiosしてまずaxiosを入れておきます

const axios = require('axios');

const API_URL = "https://www.chaplus.jp/v1/chat",
    API_KEY = "YOUR_APIKEY",

async function chat(username, msg) {//二つを合わせた関数です
    const data = choose(await hit_api(username, msg))
    return data[Math.floor(Math.random() * data.length)]
}

async function hit_api(username, msg) {//APIを叩く関数です
    try {
        const res = await axios.post(API_URL + "?apikey=" + API_KEY, {
            utterance: msg,
            username: username,
            AgentState: {//ここら辺はドキュメントを読んで調整しましょう
                agentName: "いなり(BOT)",
                age: "15"
            },
        })
        return res.data.responses
    } catch (e) {
        console.log(e)
    }

}

function choose(data) {//チャットの内容を厳選する
    return data.filter(ele => ele.score >= 0.4).length ? data.filter(ele => ele.score >= 0.4) : data
}


module.exports.chat = chat

これでchat関数を叩くとPromiseが返ってきます

最後に

これを実装したBOTはこの無名サーバーで使われています。
inarizushidiyが動いてないときは大体BOTになっていると思うので
ぜひ話しかけてみてください!!

あと無名サーバーに入ってください!!

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?