blog.sorah.jp

Making Lingr Bot

Background

I wrote an article about new Lingr bot (Japanese) about 1 year ago, but recently, Lingr API had been changed.

So I try to use new API and write a note for it.

This note is based on this official document.

What's changed?

  • Bots now can say to room on bot's own will.
  • Now JSON params will send without json= (params[:json])

Try name calling bot

require "sinatra"
require "json"

post "/hi" do
  j = JSON.parse(request.body.string)
  j["events"].map{ |e|
    if e["message"]
      "Hi, #{e["message"]["nickname"]}!"
    end
  }.compact.join("\n")+"\n"
end
  • Lingr will post a JSON to bot's endpoint.

  • JSON is like:

      {"status":"ok",
       "counter":208,
       "events":[
        {"event_id":208,
         "message":
          {"id":82,
           "room":"myroom",
           "public_session_id":"UBDH84",
           "icon_url":"http://example.com/myicon.png",
           "type":"user",
           "speaker_id":"kenn",
           "nickname":"Kenn Ejima",
           "text":"yay!",
           "timestamp":"2011-02-12T08:13:51Z",
           "local_id":"pending-UBDH84-1"}}]
    

    Quoted from the document

deploy this code to web, then register to lingr, finally invite your bot to room.

d8128a11728fcf10460e6fb4335038ba.png

Note: You can create a bot from here

Try self posting

#-*- coding: utf-8 -*-
require "sinatra"
require "json"
require "open-uri"

Thread.new do
  t = nil
  loop do
    if t.nil? || Time.now.min > t
      t = Time.now.min
      text = "#{Time.now} ですがだるいです"
      open("http://lingr.com/api/room/say?room=ROOM&bot=BOT_NAME&text=#{URI.escape(text)}&bot_verifier=YOUR_VERIFIER"){|io| io.read }
    end
    sleep 15
  end
end

post "/darui" do
  j = JSON.parse(request.body.string)
end
  • Posting API is http://lingr.com/api/room/say?room=ROOM&bot=BOT_NAME&text=TEXT&bot_verifier=YOUR_VERIFIER

    • ROOM: room id. http://lingr.com/room/ROOM_ID
    • BOT_NAME: bot id. You can reference at http://lingr.com/developer
    • TEXT: URL escaped body (It'll appear on room)
    • YOUR_VERIFIER: bot verifier. ruby -rdigest/sha1 -e'puts Digest::SHA1.hexdigest("BOT_NAME"+"BOT_SECRET")'; BOT_SECRET is at http://lingr.com/developer

Deploy this code to web, then register to lingr, finally invite your bot to room.

64b82429332f6aa5330f7f332393abcc.png

Backfilled at , Published at