本文整理汇总了Golang中github.com/djosephsen/lazlo/lib.Broker类的典型用法代码示例。如果您正苦于以下问题:Golang Broker类的具体用法?Golang Broker怎么用?Golang Broker使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Broker类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: pingRun
func pingRun(b *lazlo.Broker) {
cb := b.MessageCallback(`(?i)(ping|syn)`, true)
for {
pm := <-cb.Chan
pm.Event.Reply(randReply())
}
}
示例2: newQuestion
func newQuestion(b *lazlo.Broker, req lazlo.PatternMatch) {
lazlo.Logger.Info("new question")
qcb := b.QuestionCallback(req.Event.User, req.Match[2])
answer := <-qcb.Answer
response := fmt.Sprintf("You answered: '%s'", answer)
b.Say(response, qcb.DMChan)
}
示例3: helpRun
func helpRun(b *lazlo.Broker) {
cb := b.MessageCallback(`(?i)help`, true)
for {
pm := <-cb.Chan
go getHelp(b, &pm)
}
}
示例4: tableflipRun
func tableflipRun(b *lazlo.Broker) {
cb := b.MessageCallback(`(?i)(table)*flip(table)*`, false)
for {
pm := <-cb.Chan
pm.Event.Respond(`(╯°□°)╯︵ ┻━┻`)
}
}
示例5: getGIF
func getGIF(b *lazlo.Broker) {
g := giphy.DefaultClient
str := []string{}
cb := b.MessageCallback(`(?i)gif ((?i)search|translate) (\w+)`, false)
for {
pm := <-cb.Chan
cmd := pm.Match[1]
lazlo.Logger.Debug(cmd)
if matched, _ := regexp.MatchString(`(?i)search`, cmd); matched {
str := append(str, pm.Match[2])
lazlo.Logger.Debug(str)
resp, err := g.Search(str)
if err != nil {
lazlo.Logger.Error(err)
}
pm.Event.Reply(resp.Data[0].URL)
} else if matched, _ := regexp.MatchString(`(?i)translate`, cmd); matched {
str := append(str, pm.Match[2])
lazlo.Logger.Debug(str)
resp, err := g.Translate(str)
if err != nil {
lazlo.Logger.Error(err)
}
pm.Event.Reply(resp.Data.URL)
}
}
}
示例6: baconRun
func baconRun(b *lazlo.Broker) {
cb := b.MessageCallback(`(?i)bacon`, false)
for {
pm := <-cb.Chan
pm.Event.Respond("MMMMMMMMmmm ... omgbacon")
}
}
示例7: belittleUser
func belittleUser(broker *lazlo.Broker) {
cb := broker.MessageCallback(`(?i)(belittle) (@*[\w-]+)`, true)
for {
select {
case newReq := <-cb.Chan:
go makeFunOfUser(b, newReq)
}
}
}
示例8: newLink
func newLink(b *lazlo.Broker, path string, clickChan chan string) string {
link_cb := b.LinkCallback(path)
go func(link_cb *lazlo.LinkCallback, clickChan chan string) {
for {
<-link_cb.Chan
clickChan <- link_cb.Path
}
}(link_cb, clickChan)
return fmt.Sprintf("Ok, <%s|here> is a link on %s", link_cb.URL, path)
}
示例9: getHelp
func getHelp(b *lazlo.Broker, pm *lazlo.PatternMatch) {
dmChan := b.GetDM(pm.Event.User)
reply := `########## Modules In use: `
for _, m := range b.Modules {
if strings.Contains(m.Usage, `%HIDDEN%`) {
continue
}
usage := strings.Replace(m.Usage, `%BOTNAME%`, b.Config.Name, -1)
reply = fmt.Sprintf("%s\n%s", reply, usage)
}
b.Say(reply, dmChan)
}
示例10: catFactsRun
func catFactsRun(b *lazlo.Broker) {
cb := b.MessageCallback(`(?i)\b(cat|gato|miau|meow|garfield|lolcat)[s|z]?\b`, false)
for {
pm := <-cb.Chan
data := &facts{}
err := web.GetJSON(catFactsURL, data)
if err != nil || len(data.Facts) == 0 {
return
}
pm.Event.Respond(fmt.Sprintf("I love cats! Here's a fact: %v", data.Facts[0]))
}
}
示例11: newChoice
func newChoice(b *lazlo.Broker, clickChan chan string) string {
opt1 := b.LinkCallback(`option1`)
opt2 := b.LinkCallback(`option2`)
go func(opt1 *lazlo.LinkCallback, opt2 *lazlo.LinkCallback, clickChan chan string) {
for {
select {
case <-opt1.Chan:
clickChan <- `THIS`
case <-opt2.Chan:
clickChan <- `THAT`
}
}
}(opt1, opt2, clickChan)
return fmt.Sprintf("you can get with <%s|THIS> or you can get with <%s|THAT>", opt1.URL, opt2.URL)
}
示例12: rtmrun
func rtmrun(b *lazlo.Broker) {
for {
// get a timer callback
timer := b.TimerCallback(`*/20 * * * * * *`)
// block waiting for an alarm from the timer
<-timer.Chan
//send a ping
b.Send(&lazlo.Event{
Type: `ping`,
Text: `just pingin`,
})
}
}
示例13: gifmeRun
func gifmeRun(b *lazlo.Broker) {
cb := b.MessageCallback(`(?i)gif me (.*)`, true)
for {
pm := <-cb.Chan
search := pm.Match[1]
q := url.QueryEscape(search)
myurl := fmt.Sprintf("http://api.giphy.com/v1/gifs/random?rating=pg-13&api_key=dc6zaTOxFJmzC&tag=%s", q)
g := new(gifyout)
resp, _ := http.Get(myurl)
dec := json.NewDecoder(resp.Body)
dec.Decode(g)
pm.Event.Respond(g.Data.Image_url)
}
}
示例14: goDocRun
func goDocRun(b *lazlo.Broker) {
cb := b.MessageCallback(`(?i)^godoc ([^\s]+)$`, false)
for {
pm := <-cb.Chan
data := &godocResults{}
url, _ := url.Parse(godocSearchURL)
q := url.Query()
q.Set("q", pm.Match[1])
url.RawQuery = q.Encode()
err := web.GetJSON(url.String(), data)
if err != nil {
pm.Event.Respond(fmt.Sprintf("Error: ", err.Error()))
}
if len(data.Results) == 0 {
pm.Event.Respond(noPackagesFound)
}
a := []lazlo.Attachment{
lazlo.Attachment{
Color: "#ff0000",
Title: fmt.Sprintf(`Results of "%v"`, pm.Match[1]),
MarkdownIn: []string{"fields"},
Fields: []lazlo.AttachmentField{},
},
}
for i := 0; i < len(data.Results); i++ {
if i == 10 {
break
}
if data.Results[i].Synopsis == "" {
data.Results[i].Synopsis = "_No description_"
}
a[0].Fields = append(a[0].Fields, lazlo.AttachmentField{
Title: fmt.Sprintf("%s", data.Results[i].Path),
Value: fmt.Sprintf("%s\n<%s/%s|full GoDoc>", data.Results[i].Synopsis, godocSiteURL, data.Results[i].Path),
})
}
pm.Event.RespondAttachments(a)
}
}
示例15: loveAndWarRun
func loveAndWarRun(b *lazlo.Broker) {
cb := b.MessageCallback(`(?i)(love|insult) (@*\w+)$`, true)
for {
pm := <-cb.Chan
var reply string
act := pm.Match[1]
user := pm.Match[2]
if isme, _ := regexp.MatchString(`(?i)^me$`, user); isme {
user = b.SlackMeta.GetUserName(pm.Event.User)
}
now := time.Now()
rand.Seed(int64(now.Unix()))
if isLove, _ := regexp.MatchString(`(?i)love`, act); isLove {
reply = makeLove(user)
} else if isWar, _ := regexp.MatchString(`(?i)insult`, act); isWar {
reply = makeWar(user)
}
pm.Event.Respond(reply)
}
}