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

use cli to deal with input token #5

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
54 changes: 53 additions & 1 deletion mybot.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<[email protected]>"
app.Authors = []cli.Author{
cli.Author{
Name: "RapidLoop",
Email: "[email protected]",
},
}

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)
Expand Down Expand Up @@ -69,6 +120,7 @@ func main() {
}
}
}
*/
}

// Get the quote via Yahoo. You should replace this method to something
Expand Down
34 changes: 34 additions & 0 deletions slack.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"log"
"net/http"
"sync/atomic"
"strings"

"golang.org/x/net/websocket"
)
Expand Down Expand Up @@ -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)
}
}
}
}