11package main
22
33import (
4- "bytes"
54 "fmt"
5+ "github.com/slack-go/slack"
66 "github.com/use-go/onvif"
77 "github.com/use-go/onvif/event"
88 "gopkg.in/xmlpath.v2"
9- "io"
109 "io/ioutil"
1110 "net/http"
1211 "os"
@@ -23,9 +22,10 @@ import (
2322 2 - password
2423 3 - camera name (or location)
2524 4 - slack hook url
26- 5 - snapshot save path
27- 6 - (optional) cooldown time after motion event detected
28- 7 - (optional) json message template for sprintf ex. ./motion-poll "${<file.json}"
25+ 5 - (optional) cooldown time after motion event detected
26+ 6 - (optional) slack bot token
27+ 7 - (optional) slack channel id
28+ 8 - (optional) json message template for sprintf ex. ./motion-poll "${<file.json}"
2929*/
3030
3131const ssErrorTemplate = "Error while getting snapshot %s\n "
@@ -63,23 +63,23 @@ func main() {
6363 return
6464 }
6565
66- // get slack message from template - use default if cli arg is not given
66+ // get Slack message from template - use default if cli arg is not given
6767 camName := args [3 ]
68- var msgT string
68+ var msgT , botToken , channelID string
6969 if len (args ) > 7 {
70- msgT = args [7 ]
70+ botToken = args [6 ]
71+ channelID = args [7 ]
72+ }
73+ if len (args ) > 8 {
74+ msgT = args [8 ]
7175 } else {
72- msgT = `
73- {
74- "text" : "Motion detected at %s"
75- }
76- `
76+ msgT = "Motion detected at %s"
7777 }
7878
7979 //get cooldown time from args, default 10 seconds
8080 cooldown := 10
81- if len (args ) > 6 {
82- convInt , err := strconv .Atoi (args [6 ])
81+ if len (args ) > 5 {
82+ convInt , err := strconv .Atoi (args [5 ])
8383 if err == nil {
8484 cooldown = convInt
8585 } else {
@@ -98,46 +98,49 @@ func main() {
9898 }
9999 }
100100 fmt .Printf ("Snapshot url is %s\n " , ssUrl )
101+ slackClient := slack .New (botToken )
101102
102- // continue polling for motion events. if motion is detected, send slack notification
103+ // continue polling for motion events. if motion is detected, send Slack notification
103104 for true {
104105 r2 , _ := cam .CallMethod (event.PullMessages {})
105106 bodyBytes , _ := ioutil .ReadAll (r2 .Body )
106107 bodyS := string (bodyBytes )
107108 if strings .Contains (bodyS , "<tt:SimpleItem Name=\" IsMotion\" Value=\" true\" />" ) {
108109 msg := fmt .Sprintf (msgT , camName )
109- _ , err = http . Post (args [4 ], "aplication/json" , bytes . NewReader ([] byte ( msg )) )
110+ err = slack . PostWebhook (args [4 ], & slack. WebhookMessage { Text : msg } )
110111 if err != nil {
111112 fmt .Printf ("there was an error while posting the slack notification %s" , err )
112113 }
113- if ssUrl != "" {
114- getSnapshot (ssUrl , args [ 5 ] )
114+ if ssUrl != "" && botToken != "" && channelID != "" {
115+ getSnapshot (ssUrl , channelID , * slackClient )
115116 }
116-
117117 time .Sleep (time .Duration (cooldown ) * time .Second )
118118 }
119119 time .Sleep (1 * time .Second )
120120 }
121121
122122}
123123
124- func getSnapshot (url , path string ) {
124+ func getSnapshot (url , channelID string , slackClient slack. Client ) {
125125 r , e := http .Get (url )
126126 if e != nil {
127127 fmt .Printf (ssErrorTemplate , e )
128128 return
129129 }
130130 defer r .Body .Close ()
131-
132- file , e := os .Create (fmt .Sprintf ("%s/%s.jpeg" , path , time .Now ().Format ("20060102150405" )))
133131 if e != nil {
134132 fmt .Printf (ssErrorTemplate , e )
135133 return
136134 }
137- defer file .Close ()
138135
139- _ , e = io .Copy (file , r .Body )
140- if e != nil {
141- fmt .Printf (ssErrorTemplate , e )
136+ _ , err := slackClient .UploadFile (slack.FileUploadParameters {
137+ Reader : r .Body ,
138+ Filetype : "image/png" ,
139+ Filename : fmt .Sprintf ("%s.png" , time .Now ().Format ("20060102150405" )),
140+ Channels : []string {channelID },
141+ })
142+
143+ if err != nil {
144+ fmt .Printf ("error while posting snapshot %s" , err )
142145 }
143146}
0 commit comments