2018-11-14 23:37:57 +00:00
package communicator
2018-11-15 05:34:24 +00:00
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"strings"
"git.linuxrocker.com/mattburchett/Housekeeper/pkg/config"
)
2018-11-21 00:43:18 +00:00
type error interface {
Error ( ) string
}
2018-11-15 05:34:24 +00:00
// TelegramPost will send a message to a specific ChatID in Telegram containing the list of items to be cleaned with this cleaner.
func TelegramPost ( config config . Config , titles [ ] string ) error {
2018-11-21 00:43:18 +00:00
var err error
if len ( titles ) != 0 {
url := "https://api.telegram.org/bot" + config . TelegramToken + "/sendMessage"
values := map [ string ] string { "chat_id" : config . TelegramChatID , "text" : "The following items are to be removed from " + config . ServerName + " in 24 hours. Please go to Plex and start the title to keep it on " + config . ServerName + ". You do not need to keep watching, just hit play and load a few seconds.\n\n" + fmt . Sprintf ( "%v" , strings . Join ( titles , "\n" ) ) , "disable_notifications" : "true" }
jsonValue , _ := json . Marshal ( values )
req , err := http . NewRequest ( "POST" , url , bytes . NewBuffer ( jsonValue ) )
req . Header . Set ( "X-Custom-Header" , "Housekeeper" )
req . Header . Set ( "Content-Type" , "application/json" )
client := & http . Client { }
resp , err := client . Do ( req )
if err != nil {
panic ( err )
}
defer resp . Body . Close ( )
fmt . Println ( "response Status:" , resp . Status )
fmt . Println ( "response Headers:" , resp . Header )
body , _ := ioutil . ReadAll ( resp . Body )
fmt . Println ( "response Body:" , string ( body ) )
} else {
fmt . Println ( "There are no titles, therefore no message to send!" )
2018-11-15 05:34:24 +00:00
}
return err
}
2020-09-23 15:13:14 +00:00
// StdoutPost will relay the titles out to stdout.
func StdoutPost ( titles [ ] string ) {
if len ( titles ) != 0 {
for _ , title := range titles {
fmt . Println ( title )
}
} else {
fmt . Println ( "There are no titles. Nothing to display." )
}
}