Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

解答例: Mission 2-2 himu #122

Open
wants to merge 4 commits into
base: answer-mission-2-1
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion himu/bot/bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ func (b *Bot) Run(ctx context.Context) {
if err != nil {
log.Printf("%s: %#v\n", b.name, err)
b.out <- &model.Message{
Body: "気が乗らないパカ",
Body: "気が乗らないパカ",
Username: "jewelpet",
}
// selectから抜ける
break
Expand Down Expand Up @@ -114,3 +115,17 @@ func NewGachaBot(out chan *model.Message) *Bot {
processor: processor,
}
}

func NewTalkBot(out chan *model.Message) *Bot {
in := make(chan *model.Message)
checker := NewRegexpChecker("\\Atalk .+")
processor := &TalkProcessor{}

return &Bot{
name: "talkbot",
in: in,
out: out,
checker: checker,
processor: processor,
}
}
50 changes: 50 additions & 0 deletions himu/bot/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (

const (
keywordAPIURLFormat = "https://jlp.yahooapis.jp/KeyphraseService/V1/extract?appid=%s&sentence=%s&output=json"
talkAPIEndpoint = "https://api.a3rt.recruit-tech.co.jp/talk/v1/smalltalk"
)

type (
Expand All @@ -32,6 +33,8 @@ type (
KeywordProcessor struct{}

GachaProcessor struct{}

TalkProcessor struct{}
)

// Process は"hello, world!"というbodyがセットされたメッセージのポインタを返します
Expand Down Expand Up @@ -98,3 +101,50 @@ func (p *GachaProcessor) Process(msgIn *model.Message) (*model.Message, error) {
Username: "gatcha bot",
}, nil
}

func (p *TalkProcessor) Process(msgIn *model.Message) (*model.Message, error) {
r := regexp.MustCompile("\\Atalk (.+)\\z")
regMatches := r.FindStringSubmatch(msgIn.Body)
if len(regMatches) != 2 {
return nil, fmt.Errorf("bad message: '%s'", msgIn.Body)
}
matchedString := regMatches[1]

reqBody := make(url.Values)
reqBody.Set("apikey", env.TalkAPIKey)
reqBody.Set("query", matchedString)

type (
Result struct {
Perplexity float32 `json:"perplexity"`
Reply string `json:"reply"`
}

Response struct {
Status int `json:"status"`
Message string `json:"message"`
Results []Result `json:"results"`
}
)

var res Response
if err := post(talkAPIEndpoint, reqBody, &res); err != nil {
return nil, err
}

if len(res.Results) == 0 {
return nil, fmt.Errorf("no reply")
}

var bestReply Result
for _, r := range res.Results {
if r.Perplexity > bestReply.Perplexity {
bestReply = r
}
}

return &model.Message{
Body: bestReply.Reply,
Username: "り○なちゃん2号",
}, nil
}
2 changes: 2 additions & 0 deletions himu/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ func (s *Server) Init(dbconf, env string) error {
s.bots = append(s.bots, keywordBot)
gachaBot := bot.NewGachaBot(s.poster.In)
s.bots = append(s.bots, gachaBot)
talkBot := bot.NewTalkBot(s.poster.In)
s.bots = append(s.bots, talkBot)

return nil
}
Expand Down