func (b Bot) Dajarep(m Message, sleep time.Duration) { t := m.TextBody() dug, daj := dajarep.Dajarep(t) log.Printf("msg: %v, dajare: %+v, debug: %+v\n", t, daj, dug) if len(daj) < 1 { return } const tpl = "ねぇねぇ,%v\nいまの ```%v``` ってダジャレ?ダジャレ?" m.Text = fmt.Sprintf(tpl, b.UserName(m.UserID), t) time.Sleep(time.Duration(rand.Int63n(int64(sleep)))) b.PostMessage(m) }
// dajare sample func main() { if len(os.Args) != 2 { fmt.Fprintf(os.Stderr, "usage: bot slack-bot-token\n") os.Exit(1) } // start a websocket-based Real Time API session bot, err := slackbot.New(os.Args[1]) if err != nil { log.Fatal(err) } defer bot.Close() fmt.Println("^C exits") for { msg, err := bot.GetMessage() if err != nil { log.Printf("receive error, %v", err) } log.Printf("bot_id: %v, msguser_id: %v, msg:%+v\n", bot.ID, msg.UserID, msg) if msg.Type != "message" || msg.SubType != "" { continue } go func(m slackbot.Message) { t := m.TextBody() dug, daj := dajarep.Dajarep(t) log.Printf("msg: %v, dajare: %+v, debug: %+v\n", t, daj, dug) if len(daj) < 1 { return } m.Text = fmt.Sprintf(responseTemplate, bot.UserName(m.UserID), t) time.Sleep(botResponseSleepTime) bot.PostMessage(m) }(msg) } }
func main() { var text string var err error var encode string var debug bool var interactive bool var default_encoding string if runtime.GOOS == "windows" { default_encoding = "sjis" } else { default_encoding = "utf-8" } flag.StringVar(&encode, "e", default_encoding, "encoding") flag.BoolVar(&debug, "d", false, "debug mode") flag.BoolVar(&interactive, "i", false, "interactive mode") flag.Parse() if interactive == true { fmt.Print("> ") } else if len(flag.Args()) == 0 { text, err = readPipe() } else if flag.Arg(0) == "-" { text, err = readStdin() } else { text, err = readFileByArg(flag.Arg(0)) } if err != nil { fmt.Fprintln(os.Stderr, err.Error()) os.Exit(1) } if interactive == false { text, err := transEnc(text, encode) if err != nil { fmt.Fprintln(os.Stderr, err.Error()) os.Exit(1) } s, d := dajarep.Dajarep(text) for i := 0; i < len(s); i++ { if !debug { fmt.Println(s[i]) } else { fmt.Println(s[i] + "[" + d[i] + "]") } } } else { //interactive mode s := bufio.NewScanner(os.Stdin) for s.Scan() { if s.Err() != nil { fmt.Fprintln(os.Stderr, s.Err()) os.Exit(1) } if s.Text() == "" { break } text, err := transEnc(s.Text(), encode) if err != nil { fmt.Fprintln(os.Stderr, err.Error()) os.Exit(1) } _, d := dajarep.Dajarep(text) if len(d) > 0 { for i := 0; i < len(d); i++ { fmt.Println("-> " + d[i]) } } else { fmt.Println("") } fmt.Print("> ") } } }