10
10

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 5 years have passed since last update.

Wikipediaボットを作ってみた

Last updated at Posted at 2016-02-10

コード

explain.coffee
apiUrl = "https://ja.wikipedia.org/w/api.php?action=query&format=json&prop=extracts&redirects=1&exchars=200&explaintext=1"

module.exports = (robot) ->
  robot.respond(/explain (.*)/i, (msg) ->
    request = robot.http(apiUrl)
      .query(titles: msg.match[1])
      .get()

    request((err, res, body) ->
      if err
        msg.send("なんかエラーが起きました...")
        return

      data = JSON.parse(body)
      for id, value of data.query.pages
        if value.extract?
          respond = "Wikipediaによりますと!\n"
          respond += value.extract
          msg.send(respond)
        else
          msg.send("すみません、Wikipediaにありませんでした...")
    )
  )

実行した感じ

スクリーンショット 2016-02-10 21.25.36.png

簡単そうなので作ってみました。めちゃくちゃ役に立つ感じではないですが、パッとSlackで調べられるのであってもいいかなと思います。

10
10
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
10
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?