-
Notifications
You must be signed in to change notification settings - Fork 1
/
dialogue.go
94 lines (80 loc) · 2.46 KB
/
dialogue.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package docomo
import (
"bytes"
"encoding/json"
"golang.org/x/net/context"
)
const (
// DialogueURL docomoAPIの雑談APIのmethod
DialogueURL = "/dialogue/v1/dialogue"
)
// DialogueService API docs: https://dev.smt.docomo.ne.jp/?p=docs.api.page&api_docs_id=5
type DialogueService struct {
ctx context.Context
client *Client
}
// DialogueRequest 雑談APIのリクエスト
// Mode dialog or srtr
// CharactorID なし:デフォルト 20:関西弁 30:あかちゃん
type DialogueRequest struct {
Utt *string `json:"utt"`
Context *string `json:"context"`
Nickname *string `json:"nickname"`
NicknameYomi *string `json:"nickname_y"`
Sex *string `json:"sex"`
Bloodtype *string `json:"bloodtype"`
BirthdateY *int `json:"birthdateY"`
BirthdateM *int `json:"birthdateM"`
BirthdateD *int `json:"birthdateD"`
Age *int `json:"age"`
Constellations *string `json:"constellations"`
Place *string `json:"place"`
Mode *string `json:"mode"`
CharactorID *int `json:"t"`
}
// DialogueResponse 雑談APIのレスポンス
type DialogueResponse struct {
Context string `json:"context"`
Da string `json:"da"`
Mode string `json:"mode"`
Utt string `json:"utt"`
Yomi string `json:"yomi"`
// error時
RequestError struct {
PolicyException struct {
MessageID string `json:"messageId"`
Text string `json:"text"`
} `json:"policyException"`
} `json:"requestError"`
}
func (d *DialogueService) WithContext(ctx context.Context) *DialogueService {
d.ctx = ctx
return d
}
// Get 雑談APIを呼び出して結果を取得する.
// refreshContextがtrueの場合、DocomoClientのContextを更新する
func (d *DialogueService) Get(req DialogueRequest, refreshContext bool) (*DialogueResponse, error) {
// context有効の場合、clientで保持しているcontextを設定する
if refreshContext && req.Context != nil {
d.SetContext(*req.Context)
}
var data []byte
var err error
if data, err = json.Marshal(req); err != nil {
return nil, err
}
var dialogueRes DialogueResponse
res, err := d.client.post(d.ctx, DialogueURL, "application/json", bytes.NewReader(data), &dialogueRes)
if err != nil {
return nil, err
}
defer res.Body.Close()
if refreshContext {
d.SetContext(dialogueRes.Context)
}
return &dialogueRes, nil
}
// SetContext setter DocomoClient context.
func (d *DialogueService) SetContext(context string) {
d.client.context = context
}