├── .gitignore ├── LICENSE ├── README.md ├── builds ├── linux_arm7 │ └── slangouts ├── linux_x64 │ └── slangouts ├── linux_x86 │ └── slangouts ├── mac_x64 │ └── slangouts ├── mac_x86 │ └── slangouts ├── windows_x64 │ └── slangouts.exe └── windows_x86 │ └── slangouts.exe ├── hangoutsclient.go ├── main.go ├── slackclient.go └── slangouts.go /.gitignore: -------------------------------------------------------------------------------- 1 | # Compiled Object files, Static and Dynamic libs (Shared Objects) 2 | *.o 3 | *.a 4 | *.so 5 | 6 | # Folders 7 | _obj 8 | _test 9 | 10 | # Architecture specific extensions/prefixes 11 | *.[568vq] 12 | [568vq].out 13 | 14 | *.cgo1.go 15 | *.cgo2.c 16 | _cgo_defun.c 17 | _cgo_gotypes.go 18 | _cgo_export.* 19 | 20 | _testmain.go 21 | 22 | *.test 23 | *.prof 24 | 25 | .DS_Store -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 Yanis Pavlidis 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Slangouts 2 | Slack up front, Hangouts in the rear. 3 | 4 | ## Why? 5 | I use Slack daily at work, and lots of friends/family were using Hangouts. Didnt want to keep both open so Slangouts came to the rescue. It acts as a bridge between the 2 keeping them in sync while it runs. This means people talk to you in Hangouts, and you can reply on Slack and vice versa. 6 | 7 | ## Configuration 8 | First time your run Slangouts, it 'll ask you for access to both your Slack and Hangout accounts. Follow the on-screen instructions to complete the configuration. 9 | 10 | ## Usage 11 | You can download slangouts or build it yourself. Pre-built binaries are provided: 12 | - [Windows (64 bits)](https://raw.githubusercontent.com/gpavlidi/slangouts/master/builds/windows_x64/slangouts.exe) 13 | - [Windows (32 bits)](https://raw.githubusercontent.com/gpavlidi/slangouts/master/builds/windows_x86/slangouts.exe) 14 | - [Linux (64 bits)](https://raw.githubusercontent.com/gpavlidi/slangouts/master/builds/linux_x64/slangouts) 15 | - [Linux (32 bits)](https://raw.githubusercontent.com/gpavlidi/slangouts/master/builds/linux_x86/slangouts) 16 | - [Linux ARM7 (e.g. Pi)](https://raw.githubusercontent.com/gpavlidi/slangouts/master/builds/linux_arm7/slangouts) 17 | - [Mac (32 bits)](https://raw.githubusercontent.com/gpavlidi/slangouts/master/builds/mac_x86/slangouts) 18 | - [Mac (64 bits)](https://raw.githubusercontent.com/gpavlidi/slangouts/master/builds/mac_x64/slangouts) 19 | ``` 20 | # see all available switches 21 | ./slangouts help 22 | 23 | # run with default settings 24 | ./slangouts 25 | 26 | # pass parameters 27 | ./slangouts --config ~/.slangouts/config.json --poll 10 28 | ``` 29 | 30 | ## Building/Cross Compiling 31 | Below is mostly for me to keep handy for compiling for my Pi and other platforms. Might be useful to other people too. 32 | 33 | ``` 34 | # pull cross-compile toolchain 35 | docker pull golang:1.4.2-cross 36 | 37 | # build all versions 38 | docker run --rm -v "$GOPATH":/go -w /go/src/github.com/gpavlidi/slangouts -e GOOS=darwin -e GOARCH=amd64 -e CGO_ENABLED=0 golang:1.4.2-cross go build -v -o ./builds/mac_x64/slangouts 39 | docker run --rm -v "$GOPATH":/go -w /go/src/github.com/gpavlidi/slangouts -e GOOS=darwin -e GOARCH=386 -e CGO_ENABLED=0 golang:1.4.2-cross go build -v -o ./builds/mac_x86/slangouts 40 | docker run --rm -v "$GOPATH":/go -w /go/src/github.com/gpavlidi/slangouts -e GOOS=windows -e GOARCH=386 -e CGO_ENABLED=0 golang:1.4.2-cross go build -v -o ./builds/windows_x86/slangouts.exe 41 | docker run --rm -v "$GOPATH":/go -w /go/src/github.com/gpavlidi/slangouts -e GOOS=windows -e GOARCH=amd64 -e CGO_ENABLED=0 golang:1.4.2-cross go build -v -o ./builds/windows_x64/slangouts.exe 42 | docker run --rm -v "$GOPATH":/go -w /go/src/github.com/gpavlidi/slangouts -e GOOS=linux -e GOARCH=386 -e CGO_ENABLED=0 golang:1.4.2-cross go build -v -o ./builds/linux_x86/slangouts 43 | docker run --rm -v "$GOPATH":/go -w /go/src/github.com/gpavlidi/slangouts -e GOOS=linux -e GOARCH=amd64 -e CGO_ENABLED=0 golang:1.4.2-cross go build -v -o ./builds/linux_x64/slangouts 44 | docker run --rm -v "$GOPATH":/go -w /go/src/github.com/gpavlidi/slangouts -e GOOS=linux -e GOARCH=arm -e GOARM=7 -e CGO_ENABLED=0 golang:1.4.2-cross go build -v -o ./builds/linux_arm7/slangouts 45 | 46 | # need to clean these up every time I rebuild darwin_amd64 47 | go clean -i github.com/nlopes/slack 48 | go clean -i golang.org/x/net/websocket 49 | go clean -i github.com/codegangsta/cli 50 | 51 | # to debug cross compiling 52 | docker run --rm -it -v "$GOPATH":/go -w /go/src/github.com/gpavlidi/slangouts golang:1.4.2-cross bash 53 | GOOS=windows GOARCH=386 CGO_ENABLED=0 go build -v -o ./builds/windows_x86/slangouts.exe 54 | 55 | # copy over to Pi 56 | scp ./builds/linux_arm7/slangouts pi@gataki:~/ 57 | scp ~/.slangouts/config.json pi@gataki:~/.slangouts/config.json 58 | 59 | ``` -------------------------------------------------------------------------------- /builds/linux_arm7/slangouts: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gpavlidi/slangouts/c7f9719511e9b7cb4f25b10b81a029006dc2f3e1/builds/linux_arm7/slangouts -------------------------------------------------------------------------------- /builds/linux_x64/slangouts: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gpavlidi/slangouts/c7f9719511e9b7cb4f25b10b81a029006dc2f3e1/builds/linux_x64/slangouts -------------------------------------------------------------------------------- /builds/linux_x86/slangouts: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gpavlidi/slangouts/c7f9719511e9b7cb4f25b10b81a029006dc2f3e1/builds/linux_x86/slangouts -------------------------------------------------------------------------------- /builds/mac_x64/slangouts: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gpavlidi/slangouts/c7f9719511e9b7cb4f25b10b81a029006dc2f3e1/builds/mac_x64/slangouts -------------------------------------------------------------------------------- /builds/mac_x86/slangouts: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gpavlidi/slangouts/c7f9719511e9b7cb4f25b10b81a029006dc2f3e1/builds/mac_x86/slangouts -------------------------------------------------------------------------------- /builds/windows_x64/slangouts.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gpavlidi/slangouts/c7f9719511e9b7cb4f25b10b81a029006dc2f3e1/builds/windows_x64/slangouts.exe -------------------------------------------------------------------------------- /builds/windows_x86/slangouts.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gpavlidi/slangouts/c7f9719511e9b7cb4f25b10b81a029006dc2f3e1/builds/windows_x86/slangouts.exe -------------------------------------------------------------------------------- /hangoutsclient.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "strings" 6 | "time" 7 | 8 | "github.com/gpavlidi/go-hangups" 9 | ) 10 | 11 | type HangoutsMessage struct { 12 | senderName string 13 | message string 14 | conversationId string 15 | conversationName string 16 | } 17 | 18 | type HangoutsClient struct { 19 | PollFrequency int 20 | Session *hangups.Session 21 | Client *hangups.Client 22 | Messages chan HangoutsMessage 23 | DonePolling chan bool 24 | lastSync uint64 25 | SelfId string 26 | } 27 | 28 | func (c *HangoutsClient) Init(refreshToken string) error { 29 | c.Session = &hangups.Session{RefreshToken: refreshToken} 30 | err := c.Session.Init() 31 | if err != nil { 32 | return err 33 | } 34 | 35 | c.Client = &hangups.Client{Session: c.Session} 36 | c.Messages = make(chan HangoutsMessage) 37 | c.DonePolling = make(chan bool) 38 | 39 | return nil 40 | } 41 | 42 | func (c *HangoutsClient) StartPolling() error { 43 | // find whoami and seed the sync timestamp to current time 44 | getSelfInfo, _ := c.Client.GetSelfInfo() 45 | c.lastSync = *getSelfInfo.ResponseHeader.CurrentServerTime 46 | c.SelfId = *getSelfInfo.SelfEntity.Id.GaiaId 47 | 48 | go func() { 49 | ticker := time.NewTicker(time.Second * time.Duration(c.PollFrequency)) 50 | for { 51 | select { 52 | case <-ticker.C: 53 | c.poll() 54 | case <-c.DonePolling: 55 | return 56 | } 57 | } 58 | }() 59 | 60 | return nil 61 | } 62 | 63 | func (c *HangoutsClient) StopPolling() { 64 | c.DonePolling <- true 65 | return 66 | } 67 | 68 | func (c *HangoutsClient) SendMessage(msg HangoutsMessage) error { 69 | // mark all events in this conversation as read 70 | _, _ = c.Client.UpdateWatermark(msg.conversationId, c.lastSync) 71 | 72 | _, err := c.Client.SendChatMessage(msg.conversationId, msg.message) 73 | return err 74 | 75 | } 76 | 77 | func (c *HangoutsClient) poll() { 78 | newEvents, err := c.Client.SyncAllNewEvents(c.lastSync, 1048576) //1 MB 79 | if err != nil { 80 | return 81 | } 82 | c.lastSync = *newEvents.ResponseHeader.CurrentServerTime 83 | 84 | for _, conversation := range newEvents.ConversationState { 85 | 86 | //find or generate conversation name 87 | conversationName := "" 88 | if conversation.Conversation.Name != nil { 89 | conversationName = fmt.Sprintf("hangouts-%s", *conversation.Conversation.Name) 90 | } else { 91 | participants := make([]string, 0) 92 | for _, participant := range conversation.Conversation.ParticipantData { 93 | // skip my name from participants list 94 | if *participant.Id.GaiaId == c.SelfId { 95 | continue 96 | } 97 | participants = append(participants, *participant.FallbackName) 98 | } 99 | conversationName = fmt.Sprintf("hangouts-%s", strings.Join(participants, ",")) 100 | } 101 | 102 | for _, event := range conversation.Event { 103 | senderId := *event.SenderId.GaiaId 104 | 105 | // dont echo my msgs 106 | if senderId == c.SelfId { 107 | continue 108 | } 109 | 110 | // find sender name 111 | senderName := "Unknown" 112 | for _, participant := range conversation.Conversation.ParticipantData { 113 | if *participant.Id.GaiaId == senderId { 114 | senderName = *participant.FallbackName 115 | break 116 | } 117 | } 118 | 119 | // reconstruct msg text 120 | message := "" 121 | for _, segment := range event.ChatMessage.MessageContent.Segment { 122 | message = fmt.Sprint(message, *segment.Text) 123 | } 124 | 125 | // add attachments if they exist 126 | for _, attachment := range event.ChatMessage.MessageContent.Attachment { 127 | if attachment.EmbedItem != nil { 128 | if attachment.EmbedItem.Id != nil { 129 | message = fmt.Sprint(message, "\n", *attachment.EmbedItem.Id) 130 | } 131 | if attachment.EmbedItem.PlusPhoto != nil { 132 | message = fmt.Sprint(message, "\n", *attachment.EmbedItem.PlusPhoto.Url) 133 | } 134 | } 135 | } 136 | 137 | // send message to channel 138 | c.Messages <- HangoutsMessage{message: message, senderName: senderName, conversationName: conversationName, conversationId: *conversation.ConversationId.Id} 139 | } 140 | } 141 | return 142 | } 143 | -------------------------------------------------------------------------------- /main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "os" 5 | 6 | "github.com/codegangsta/cli" 7 | ) 8 | 9 | func main() { 10 | app := cli.NewApp() 11 | app.Name = "slangouts" 12 | app.Usage = "Slack up front, Hangouts in the rear." 13 | app.Version = "0.0.1" 14 | app.Author = "gpavlidi" 15 | app.Email = "https://github.com/gpavlidi/slangouts" 16 | app.Flags = []cli.Flag{ 17 | cli.IntFlag{ 18 | Name: "poll", 19 | Value: 10, 20 | Usage: "polling frequency for new Hangout messages", 21 | }, 22 | cli.StringFlag{ 23 | Name: "config", 24 | Value: "~/.slangouts/config.json", 25 | Usage: "path to config file", 26 | }, 27 | } 28 | app.Action = func(c *cli.Context) { 29 | path := c.String("config") 30 | // if config path not specifically set, let user.HomeDir find it 31 | if !c.IsSet("config") { 32 | path = "" 33 | } 34 | runSlangouts(c.Int("poll"), path) 35 | } 36 | 37 | app.Run(os.Args) 38 | 39 | } 40 | -------------------------------------------------------------------------------- /slackclient.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "log" 6 | "regexp" 7 | "strconv" 8 | "strings" 9 | "time" 10 | 11 | "github.com/nlopes/slack" 12 | ) 13 | 14 | type SlackClient struct { 15 | Client *slack.Client 16 | Token string 17 | SelfId string 18 | Messages chan HangoutsMessage 19 | DonePolling chan bool 20 | groups []slack.Group 21 | } 22 | 23 | func (c *SlackClient) Init(apiToken string) error { 24 | c.Token = apiToken 25 | c.Client = slack.New(c.Token) 26 | //c.Client.SetDebug(true) 27 | authResponse, err := c.Client.AuthTest() 28 | if err != nil { 29 | // ask the user for the slack token 30 | log.Println("Invalid Slack Token. Please navigate to the below address, create a Token, confirm it and paste it below\n") 31 | fmt.Println("https://api.slack.com/docs/oauth-test-tokens#test_token_generator\n") 32 | fmt.Print("Slack Token: ") 33 | fmt.Scanln(&c.Token) 34 | c.Client = slack.New(c.Token) 35 | authResponse, err = c.Client.AuthTest() 36 | return err 37 | } 38 | 39 | c.SelfId = authResponse.UserID 40 | c.Messages = make(chan HangoutsMessage) 41 | c.DonePolling = make(chan bool) 42 | 43 | return nil 44 | } 45 | 46 | func (c *SlackClient) SendMessage(msg HangoutsMessage) error { 47 | var err error 48 | // check local group cache first 49 | existingGroup := c.GetGroupByPurpose(msg.conversationId) 50 | 51 | // if not found update cache and check again 52 | if existingGroup == nil { 53 | c.UpdateGroups() 54 | existingGroup = c.GetGroupByPurpose(msg.conversationId) 55 | } 56 | 57 | // nowhere to be found, create a new group 58 | if existingGroup == nil { 59 | existingGroup, err = c.Client.CreateGroup(fmt.Sprint("hangouts-", msg.conversationId)) 60 | if err != nil { 61 | return err 62 | } 63 | c.groups = append(c.groups, *existingGroup) 64 | 65 | topic, err := c.Client.SetGroupTopic(existingGroup.ID, msg.conversationName) 66 | if err != nil { 67 | return err 68 | } 69 | existingGroup.Topic.Value = topic 70 | 71 | purpose, err := c.Client.SetGroupPurpose(existingGroup.ID, msg.conversationId) 72 | if err != nil { 73 | return err 74 | } 75 | existingGroup.Purpose.Value = purpose 76 | } else { 77 | if existingGroup.IsArchived { 78 | err = c.Client.UnarchiveGroup(existingGroup.ID) 79 | if err != nil { 80 | return err 81 | } 82 | existingGroup.IsArchived = false 83 | } 84 | } 85 | 86 | _, _, err = c.Client.PostMessage(existingGroup.ID, msg.message, slack.PostMessageParameters{Username: msg.senderName}) 87 | return err 88 | } 89 | 90 | func (c *SlackClient) GetGroupById(id string) *slack.Group { 91 | for _, group := range c.groups { 92 | if group.ID == id { 93 | return &group 94 | } 95 | } 96 | return nil 97 | } 98 | 99 | func (c *SlackClient) GetGroupByPurpose(purpose string) *slack.Group { 100 | for _, group := range c.groups { 101 | if group.Purpose.Value == purpose { 102 | return &group 103 | } 104 | } 105 | return nil 106 | } 107 | 108 | func (c *SlackClient) UpdateGroups() error { 109 | var err error 110 | c.groups, err = c.Client.GetGroups(false) 111 | return err 112 | } 113 | 114 | func (c *SlackClient) StartPolling() error { 115 | rtm := c.Client.NewRTM() 116 | go rtm.ManageConnection() 117 | 118 | // slack for some reason echos the last message on connect 119 | isFirstMsg := true 120 | go func() { 121 | for { 122 | select { 123 | case <-c.DonePolling: 124 | return 125 | case msg := <-rtm.IncomingEvents: 126 | switch ev := msg.Data.(type) { 127 | case *slack.ConnectedEvent: 128 | // get available groups at slack so we can correlate them to a hangout id 129 | c.groups = ev.Info.Groups 130 | case *slack.MessageEvent: 131 | // for first message specifically, check if it's older 132 | // slack for some reason echos the last msg 133 | if isFirstMsg { 134 | isFirstMsg = false 135 | timestamp, _ := strconv.ParseFloat(ev.Timestamp, 64) 136 | if int64(timestamp) < time.Now().Unix() { 137 | log.Println("Slack: Skipping first message since it appears old:", ev.Text) 138 | continue 139 | } 140 | } 141 | 142 | if ev.User == c.SelfId { 143 | group := c.GetGroupById(ev.Channel) 144 | if group == nil { 145 | log.Println("Slack: Got msg from a group (", ev.Channel, ") not cached locally. Refreshing groups.") 146 | c.UpdateGroups() 147 | group = c.GetGroupById(ev.Channel) 148 | if group == nil { 149 | log.Println("Slack: Cant find group", ev.Channel, "even after updating Groups. Skipping message:", ev.Text) 150 | } 151 | } 152 | if group != nil && strings.Contains(group.Topic.Value, "hangouts-") { 153 | // links are enclosed in <> - remove them 154 | linkRegex := regexp.MustCompile("^<(.*)>$") 155 | pipeRegex := regexp.MustCompile("(.*)\\|(.*)") 156 | parsedText := ev.Text 157 | if linkRegex.MatchString(parsedText) { 158 | parsedText = pipeRegex.ReplaceAllString(linkRegex.ReplaceAllString(parsedText, "$1"), "$1") 159 | } 160 | 161 | c.Messages <- HangoutsMessage{message: parsedText, conversationId: group.Purpose.Value} 162 | } 163 | } 164 | case *slack.RTMError: 165 | log.Println("Slack Error: ", ev.Error()) 166 | case *slack.InvalidAuthEvent: 167 | log.Fatal("Slack Error: Invalid Slack credentials") 168 | } 169 | } 170 | } 171 | }() 172 | 173 | return nil 174 | } 175 | -------------------------------------------------------------------------------- /slangouts.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "encoding/json" 5 | "io/ioutil" 6 | "log" 7 | "os" 8 | "os/signal" 9 | "path" 10 | "syscall" 11 | ) 12 | 13 | type SlangoutsApp struct { 14 | configFile string 15 | config SlangoutsConfig 16 | hangoutsClient *HangoutsClient 17 | slackClient *SlackClient 18 | } 19 | 20 | type SlangoutsConfig struct { 21 | HangoutsRefreshToken string `json:"hangoutsRefreshToken"` 22 | SlackApiToken string `json:"slackApiToken"` 23 | } 24 | 25 | func runSlangouts(hangoutsPollFreq int, configPath string) { 26 | if configPath == "" { 27 | configPath = getConfigPath() 28 | } 29 | app := &SlangoutsApp{configFile: configPath, hangoutsClient: &HangoutsClient{PollFrequency: hangoutsPollFreq}, slackClient: &SlackClient{}} 30 | app.Run() 31 | } 32 | 33 | func (app *SlangoutsApp) Run() { 34 | err := app.loadConfig() 35 | if err != nil { 36 | log.Println(err, ". Generating blank config...") 37 | app.saveConfig() 38 | } 39 | 40 | // check Slack token 41 | err = app.slackClient.Init(app.config.SlackApiToken) 42 | if err != nil { 43 | log.Fatal(err) 44 | } 45 | 46 | // update Slack token if changed 47 | if app.slackClient.Token != app.config.SlackApiToken { 48 | app.config.SlackApiToken = app.slackClient.Token 49 | app.saveConfig() 50 | log.Println("Slack token changed. Saving it.") 51 | } 52 | 53 | // check Hangouts token 54 | err = app.hangoutsClient.Init(app.config.HangoutsRefreshToken) 55 | if err != nil { 56 | //re-try without a refresh token 57 | app.config.HangoutsRefreshToken = "" 58 | err = app.hangoutsClient.Init(app.config.HangoutsRefreshToken) 59 | if err != nil { 60 | log.Fatal(err) 61 | } 62 | } 63 | 64 | // update Hangouts token if changed 65 | if app.hangoutsClient.Session.RefreshToken != app.config.HangoutsRefreshToken { 66 | app.config.HangoutsRefreshToken = app.hangoutsClient.Session.RefreshToken 67 | app.saveConfig() 68 | log.Println("Hangouts refresh token changed. Saving it.") 69 | } 70 | 71 | // start Slack Polling 72 | app.slackClient.StartPolling() 73 | //defer app.slackClient.StopPolling() 74 | 75 | // start Hangouts Polling 76 | app.hangoutsClient.StartPolling() 77 | defer app.hangoutsClient.StopPolling() 78 | 79 | // catch Ctrl+C 80 | signals := make(chan os.Signal, 1) 81 | signal.Notify(signals, os.Interrupt, os.Kill, syscall.SIGABRT) 82 | 83 | // event loop 84 | for { 85 | select { 86 | case <-signals: 87 | return 88 | case msg := <-app.hangoutsClient.Messages: 89 | log.Println("Hangouts: ", msg) 90 | err = app.slackClient.SendMessage(msg) 91 | if err != nil { 92 | log.Println("Slack SendMessage", msg, "failed", err) 93 | } 94 | case msg := <-app.slackClient.Messages: 95 | log.Println("Slack: ", msg) 96 | err = app.hangoutsClient.SendMessage(msg) 97 | if err != nil { 98 | log.Println("Hangout SendMessage", msg, "failed", err) 99 | } 100 | } 101 | } 102 | } 103 | 104 | func (app *SlangoutsApp) loadConfig() error { 105 | b, err := ioutil.ReadFile(app.configFile) 106 | if err == nil { 107 | return json.Unmarshal(b, &app.config) 108 | } 109 | return err 110 | } 111 | 112 | func (app *SlangoutsApp) saveConfig() error { 113 | j, err := json.MarshalIndent(&app.config, "", "\t") 114 | if err == nil { 115 | os.MkdirAll(path.Dir(app.configFile), os.ModePerm) 116 | return ioutil.WriteFile(app.configFile, j, 0600) 117 | } 118 | return err 119 | } 120 | 121 | func getConfigPath() string { 122 | workingDir := "." 123 | for _, name := range []string{"HOME", "USERPROFILE"} { // *nix, windows 124 | if dir := os.Getenv(name); dir != "" { 125 | workingDir = dir 126 | } 127 | } 128 | 129 | return path.Join(workingDir, ".slangouts", "config.json") 130 | } 131 | 132 | func OrDie(e error) { 133 | if e != nil { 134 | log.Fatal(e) 135 | } 136 | } 137 | --------------------------------------------------------------------------------