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

Answer for Mission 2-2 #114

Open
wants to merge 1 commit into
base: master
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
16 changes: 16 additions & 0 deletions original/bot/bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,19 @@ func NewKeywordBot(out chan *model.Message) *Bot {
processor: processor,
}
}

// NewTalkBot は...
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,
}
}
34 changes: 34 additions & 0 deletions original/bot/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (

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

type (
Expand All @@ -29,6 +30,9 @@ type (

// KeywordProcessor はメッセージ本文からキーワードを抽出するprocessorの構造体です
KeywordProcessor struct{}

// TalkProcessor は...
TalkProcessor struct{}
)

// Process は"hello, world!"というbodyがセットされたメッセージのポインタを返します
Expand Down Expand Up @@ -82,3 +86,33 @@ func (p *KeywordProcessor) Process(msgIn *model.Message) (*model.Message, error)
Body: "キーワード:" + strings.Join(keywords, ", "),
}, nil
}

// Process は...
func (p *TalkProcessor) Process(msgIn *model.Message) (*model.Message, error) {
r := regexp.MustCompile("\\Atalk (.*)\\z")
matchedStrings := r.FindStringSubmatch(msgIn.Body)
text := matchedStrings[1]

params := url.Values{}
params.Set("apikey", env.TalkAPIKey)
params.Add("query", text)

res := &struct {
Status int64 `json:status`
Message string `json:message`
Results []struct {
Perplexity float64 `json:perplexity`
Reply string `json:reply`
} `json:results`
}{}

post(talkAPIURL, params, res)

if res.Status != 0 {
return nil, fmt.Errorf("%#v", res)
}

return &model.Message{
Body: res.Results[0].Reply,
}, nil
}
3 changes: 3 additions & 0 deletions original/env/env.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,7 @@ package env
const (
// KeywordAPIAppID はYahoo!デベロッパーネットワーク(https://developer.yahoo.co.jp/)のアプリケーションIDです
KeywordAPIAppID = ""

// TalkAPIKey はリクルートのTalk APIのAPI Keyです
TalkAPIKey = ""
)
4 changes: 4 additions & 0 deletions original/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ func (s *Server) Init(dbconf, env string) error {
keywordBot := bot.NewKeywordBot(s.poster.In)
s.bots = append(s.bots, keywordBot)

// Mission 2-2
talkBot := bot.NewTalkBot(s.poster.In)
s.bots = append(s.bots, talkBot)

return nil
}

Expand Down