More Related Content
Similar to Go言語でBot開発やってみた (20)
More from Akihiko Horiuchi (17)
Go言語でBot開発やってみた
- 4. Go言語の概要
● Go言語の概要
○ 2009年にGoogleによって開発スタート
○ シンプルな言語仕様,低い学習コスト
○ HTTPやJSONのなどの豊富な標準パケージ
○ 高速な動作,クロスコンパイルのサポート
○ goroutine と channel による並列処理
● 開発者
○ Ken Thompson(C言語)
○ Rob Pike(UTF-8)
○ Brad Fitzpatrick(memcached)
15/08/22 4
- 5. Go言語で嬉しいこと
● 常に綺麗なコード
○ 標準のコーディング規約が定義
○ go fmt で自動的に整形,エディタの保存時に実行
○ 未使用の変数があるとコンパイルエラー
● 高速な動作、楽なデプロイ
○ C言語並みの実行速度,バックエンドでの採用
○ バイナリ1つでデプロイ,すぐに動作可能
15/08/22 5
$ go build hoge.go
# command-line-arguments
./hoge.go:4: hoge declared and not used
- 6. hello world
15/08/22 6
package main // パッケージの宣言
import "fmt" // インポート
func main() {
var message string = "hello world" // 変数の宣言
fmt.Println(message) // 標準出力
}
$ go run hello.go
hello world
$ go build hello.go
$ ./hello
hello world
- 8. Go言語のBotフレームワーク
● Botフレームワーク
○ プラグインで機能を拡張できるもの
○ アダプタ,ストア(ブレイン)があればBetter
● GitHubで探してみた
○ danryan/hal :アダプタとストアあり,機能も充実
○ brettbuddin/victor:アダプタとストアあり,活発に開発中
○ groob/radigast :Slack限定,victorベース
○ kyokomi/slackbot :Slack限定,グノシーの方が開発
15/08/22 8
- 9. halとvictorの比較
15/08/22 9
hal victor
★ 150 113
アダプタ
Shell,IRC
HipChat,Slack(WebHook)
Shell,Campfire
HipChat,Slack(RealTime)
ストア Memory,Redis Memory,BoltDB
ヘルプ × ⃝(FogCreek)
HTTP ⃝ ×
Hear ⃝ ×
Reply ⃝ ⃝(FogCreek)
- 11. dialogueHandler()
15/08/22 11
func dialogueHandler(res *hal.Response) error {
// インスタンス生成
d := docomo.NewClient(os.Getenv("DOCOMO_DIALOGUE_API_KEY"))
// ユーザーの発話を設定
dialogue := docomo.DialogueRequest{
Utt: &res.Match[1], // hal
Utt: &s.Params()[0], // victor
}
// docomo 雑談対話APIを呼出
get, _ := d.Dialogue.Get(dialogue, true)
return res.Reply(get.Utt) // hal
s.Chat().Send(s.Message().ChannelID(), get.Utt) // victor
}
- 12. sayHandler()
15/08/22 12
func sayHandler(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
body, _ := ioutil.ReadAll(r.Body)
robot.Adapter.Send(nil, string(body)) // hal
bot.Chat().Send("0", string(body)) // victor
}
- 13. danryan/hal
15/08/22 13
var robot *hal.Robot
func main() {
robot, _ = hal.NewRobot() // インスタンス生成
// ハンドラー登録
robot.Handle(hal.Respond(`(.+)`, dialogueHandler))
hal.Router.HandleFunc("/say", sayHandler)
robot.Run() // hal実行
}
- 14. brettbuddin/victor
15/08/22 14
var bot victor.Robot
func main() {
// インスタンス生成
bot = victor.New(victor.Config{
Name: "victor",
ChatAdapter: "shell",
})
dialogueHandler(bot) // ハンドラー登録
go bot.Run() // victor実行
http.HandleFunc("/say", sayHandler) // パス登録
http.ListenAndServe(":9000", nil) // HTTP待受
}