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

[WIP]Team d #139

Open
wants to merge 9 commits 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
10 changes: 5 additions & 5 deletions ranbo/assets/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@
<button v-on:click="cancelEdit">Cancel</button>
</div>
</div>
<div class="message-body" v-else>
<span>{{ body }} - {{ username }}</span>
<span class="action-button u-pull-right" v-on:click="edit">&#9998;</span>
<span class="action-button u-pull-right" v-on:click="remove">&#10007;</span>
<div class="message-body" style="position:relative;" v-else>
<span style="display:block;"><p v-html="body"></p> - {{username}}</span>
<span style="position:absolute; top:0; right:0;" class="action-button u-pull-right" v-on:click="edit">&#9998;</span>
<span style="position:absolute; top:0; right:1em;" class="action-button u-pull-right" v-on:click="remove">&#10007;</span>
</div>
</div>
`,
Expand All @@ -49,7 +49,7 @@
this.cancelEdit()
})
}
}
}
});

const app = new Vue({
Expand Down
34 changes: 34 additions & 0 deletions ranbo/bot/bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,3 +116,37 @@ 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,
// }
//}
// youtube bot
func NewYoutubeBot(out chan *model.Message) *Bot {
in := make(chan *model.Message)

// youtube ~
checker := NewRegexpChecker("\\Ayoutube .+")

processor := &YoutubeProcessor{}

return &Bot{
name: "youtubebot",
in: in,
out: out,
checker: checker,
processor: processor,
}

}
86 changes: 85 additions & 1 deletion ranbo/bot/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import (

const (
keywordAPIURLFormat = "https://jlp.yahooapis.jp/KeyphraseService/V1/extract?appid=%s&sentence=%s&output=json"
talkAPIURLFormat = "https://api.a3rt.recruit-tech.co.jp/talk/v1/smalltalk"
youtubeAPIURLFormat = "https://www.googleapis.com/youtube/v3/search?part=id&type=video&key=%s&q=%s"
)

type (
Expand All @@ -30,7 +32,12 @@ type (
// KeywordProcessor はメッセージ本文からキーワードを抽出するprocessorの構造体です
KeywordProcessor struct{}

TalkProcessor struct{}

GachaProcessor struct{}

// youtube bot
YoutubeProcessor struct{}
)

// Process は"hello, world!"というbodyがセットされたメッセージのポインタを返します
Expand Down Expand Up @@ -70,6 +77,7 @@ func (p *KeywordProcessor) Process(msgIn *model.Message) (*model.Message, error)

type keywordAPIResponse map[string]interface{}
var response keywordAPIResponse

get(requestURL, &response)

keywords := make([]string, 0, len(response))
Expand All @@ -85,7 +93,52 @@ func (p *KeywordProcessor) Process(msgIn *model.Message) (*model.Message, error)
}, nil
}

func (p *GachaProcessor) Process(msgIn *model.Message) (*model.Message, error){
// youtube bot
func (p *YoutubeProcessor) Process(msgIn *model.Message) (*model.Message, error) {
r := regexp.MustCompile("\\Ayoutube (.+)")
matchedStrings := r.FindStringSubmatch(msgIn.Body)
if len(matchedStrings) != 2 {
return nil, fmt.Errorf("bad message: %s", msgIn.Body)
}

text := matchedStrings[1]

requestURL := fmt.Sprintf(youtubeAPIURLFormat, env.YoutubeAPIAppID, url.QueryEscape(text))

type youtubeAPIResponse map[string]interface{}

res := &struct {
Items []struct {
Id struct {
VideoId string `json:videoid`
} `json:id`
} `json:items`
}{}
get(requestURL, res)

fmt.Println(res.Items)

// set videos this
var maxlen int = len(res.Items)
if maxlen > 3 {
maxlen = 3
}

var videos string
for i := 0; i < maxlen; i++ {
if i == 0 {
videos += "<iframe width=\"560\" height=\"315\" src=\"https://www.youtube.com/embed/" + res.Items[i].Id.VideoId + "\" frameborder=\"0\" allow=\"autoplay; encrypted-media\" allowfullscreen></iframe><br>"
continue
}
videos += "<a href=\"https://www.youtube.com/watch?v=" + res.Items[i].Id.VideoId + "\" target=\"_blank\">" + res.Items[i].Id.VideoId + "</a><br>"
}

return &model.Message{
Body: videos,
}, nil
}

func (p *GachaProcessor) Process(msgIn *model.Message) (*model.Message, error) {
fortunes := []string{
"SSR",
"SR",
Expand All @@ -97,3 +150,34 @@ func (p *GachaProcessor) Process(msgIn *model.Message) (*model.Message, error){
Body: result,
}, nil
}

// func (p *KeywordProcessor) Talk(msgIn *model.Message) (*model.Message, error) {
// r := regexp.MustCompile("\\Atalk (.+)")
// matchedStrings := r.FindStringSubmatch(msgIn.Body)
// if len(matchedStrings) != 2 {
// return nil, fmt.Errorf("bad message: %s", msgIn.Body)
// }
//
// text := matchedStrings[1]
//
// // requestURL := fmt.Sprintf(keywordAPIURLFormat, env.TalkAPIAppID, url.QueryEscape(text), "おはよう")
//
// type keywordAPIResponse map[string]interface{}
// var response keywordAPIResponse
// val := url.Values{}
// val.Set("apikey", env.KeywordAPIAppID)
// val.Add("query", "おはよう")
//
// post(talkAPIURLFormat, val, &response)
// talks := make([]string, 0, len(response))
// for k, v := range response {
// if k == "Error" {
// return nil, fmt.Errorf("%#v", v)
// }
// talks = append(talks, k)
// }
//
// return &model.Message{
// Body: "キーワード:" + strings.Join(talks, ", "),
// }, nil
// }
1 change: 1 addition & 0 deletions ranbo/env/env.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ package env
const (
// KeywordAPIAppID はYahoo!デベロッパーネットワーク(https://developer.yahoo.co.jp/)のアプリケーションIDです
KeywordAPIAppID = ""
YoutubeAPIAppID = ""
)
2 changes: 2 additions & 0 deletions ranbo/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)
youtubeBot := bot.NewYoutubeBot(s.poster.In)
s.bots = append(s.bots, youtubeBot)

return nil
}
Expand Down