diff --git a/mybot.go b/mybot.go index d59be84..2a7865a 100644 --- a/mybot.go +++ b/mybot.go @@ -3,6 +3,7 @@ mybot - Illustrative Slack bot in Go Copyright (c) 2015 RapidLoop +Copyright (c) 2017 sndnvaps Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal @@ -28,13 +29,63 @@ package main import ( "encoding/csv" "fmt" - "log" + //"log" "net/http" "os" "strings" + "github.com/urfave/cli" + "time" ) + +var ( + Token string +) + +func actionStartSlack(c *cli.Context) error { + if c.String("token") != "" || c.String("t") != "" { + slackRun(Token) + } else { + cli.ShowCommandHelp(c, "start") + } + return nil +} + + func main() { + app := cli.NewApp() + app.Name = "aiicySlackBot" + app.Usage = "Slack bot to get the stock infomation" + app.Version = "0.5.0" + app.Compiled = time.Now() + app.Copyright = "Copyright (c) 2015 RapidLoop\n\t Copyright (c) 2017 sndnvaps" + app.Authors = []cli.Author{ + cli.Author{ + Name: "RapidLoop", + Email: "mdevan@gaia.local", + }, + } + + app.Commands = []cli.Command{ + { + Name: "start", + Usage: "start slack bot", + Flags: []cli.Flag{ + cli.StringFlag{ + Name: "token,t", + Usage: "myslack bot token", + Destination: &Token, + }, + }, + Action: actionStartSlack, + }, + } + + if err := app.Run(os.Args); err != nil { + os.Exit(1) + } + +/* if len(os.Args) != 2 { fmt.Fprintf(os.Stderr, "usage: mybot slack-bot-token\n") os.Exit(1) @@ -69,6 +120,7 @@ func main() { } } } +*/ } // Get the quote via Yahoo. You should replace this method to something diff --git a/slack.go b/slack.go index 3ef645c..f0a8eb5 100644 --- a/slack.go +++ b/slack.go @@ -32,6 +32,7 @@ import ( "log" "net/http" "sync/atomic" + "strings" "golang.org/x/net/websocket" ) @@ -121,3 +122,36 @@ func slackConnect(token string) (*websocket.Conn, string) { return ws, id } + + +func slackRun(token string) { + + ws, id := slackConnect(token) + fmt.Println("mybot ready, ^C exits") + + for { + // read each incoming message + m, err := getMessage(ws) + if err != nil { + log.Fatal(err) + } + + // see if we're mentioned + if m.Type == "message" && strings.HasPrefix(m.Text, "<@"+id+">") { + // if so try to parse if + parts := strings.Fields(m.Text) + if len(parts) == 3 && parts[1] == "stock" { + // looks good, get the quote and reply with the result + go func(m Message) { + m.Text = getQuote(parts[2]) + postMessage(ws, m) + }(m) + // NOTE: the Message object is copied, this is intentional + } else { + // huh? + m.Text = fmt.Sprintf("sorry, that does not compute\n") + postMessage(ws, m) + } + } + } +} \ No newline at end of file