LoginSignup
3
1

More than 5 years have passed since last update.

Mattermostでrubybot

Last updated at Posted at 2017-01-24

Mattermost

mattermost

Slackライクなチャット。
このあたりの記事を参考。
バージョンは 3.4 を利用。
サクっとbotを実現する。

Incoming Webhook

追加したフックのURLに対して何かポストするとメッセージが表示される。

コード

このままやるとできる
http://qiita.com/tbpgr/items/b7529021c384c3f3bd89

require 'net/http'
require 'uri'
require 'json'

# send
class SlackWraper
  def self.post(text)
   # ポスト
   #data = { "text" => text }
   # チャンネル
   #data = { "channel" => "off-topic" , "text" => text }
   # ユーザー
   #data = { "username" => "Hoge", "text" => text }
   request_url = 'http://IPアドレス:8065/hooks/キー' # Incoming Webhookを追加した後確認できるURL
   uri = URI.parse(request_url)
   Net::HTTP.post_form(uri, {"payload" => data.to_json})  
 end
end
SlackWraper.post("=Message=")


data = { "text" => "あいうえお" }
data = { "channel" => "off-topic", "text" => "あいうえお" }
data = { "username" => "Hoge", "text" => "あいうえお" }

{ "payload" => data.to_json } をおくれば反応してくれる。

Outcoming Webhook

簡単にやるなら以下の通り
http://sbkro.hatenablog.jp/entry/20120114/1326553671

require 'webrick'

# サンプルサーブレット
class ServletAction < WEBrick::HTTPServlet::AbstractServlet

    def do_POST (req, res)
        # このあたりに処理が来るのでやりたいようにやる
        res.body = req.body
        print(req.body)
    end
end

# サーバ起動処理
srv = WEBrick::HTTPServer.new({
    :DocumentRoot => '.',
    :BindAddress => 'IPアドレス',
    :Port => 9999})

srv.mount('/param', ServletAction)

trap("INT"){ srv.shutdown }
srv.start

ポストされる内容(Mattermostから送られてくる内容)

名前
channel_id ハッシュ
channel_name town-square
post_id ハッシュ
team_domain チーム名
team_id ハッシュ
text %E3T81%82 ※(あ)
timestamp 1476320873
token ハッシュ
trigger_word %E3%81%82 ※(あ)
user_id ハッシュ
user_name ???

Contesnt-Typeをapplication/jsonにすると以下のような感じでPOSTされてくる

{
    "channel_id":"ハッシュ",
    "channel_name":"town-square",
    "post_id":"ハッシュ",
    "team_domain":"チーム名",
    "team_id":"ハッシュ",
    "text":"%E3%81%82",
    "timestamp":"1476320873",
    "token":"ハッシュ",
    "trigger_word":"%E3%81%82",
    "user_id":"ハッシュ",
    "user_name":"????",
}
3
1
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
3
1