-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #29 from Divi09112/discord
Add discord support
- Loading branch information
Showing
6 changed files
with
144 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package discord | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/devclub-iitd/DeployBot/src/helper" | ||
log "github.com/sirupsen/logrus" | ||
) | ||
|
||
var ( | ||
// postMessageHookURL is the webhook url for posting messages on discord | ||
postMessageHookURL string | ||
serverURL string | ||
) | ||
|
||
func init() { | ||
postMessageHookURL = helper.Env("DISCORD_MESSAGE_WEBHOOK", "None") | ||
if postMessageHookURL == "None" { | ||
log.Fatal("DISCORD_MESSAGE_WEBHOOK not present in environment, Exiting") | ||
} | ||
serverURL = helper.Env("SERVER_URL", "https://listen.devclub.iitd.ac.in") | ||
} | ||
|
||
type Embed struct { | ||
Title string `json:"title"` | ||
Color int `json:"color"` | ||
Fields []interface{} `json:"fields"` | ||
} | ||
|
||
type Message struct { | ||
Content string `json:"content"` | ||
Embeds []*Embed `json:"embeds"` | ||
} | ||
|
||
func newMessage(callbackID, action, result string, fields []interface{}) Message { | ||
var callbackField interface{} | ||
callbackField = map[string]interface{}{ | ||
"name": "Action ID", | ||
"value": callbackID, | ||
"inline": true, | ||
} | ||
fields = append([]interface{}{callbackField}, fields...) | ||
embed := &Embed{Fields: fields} | ||
switch result { | ||
case "success": | ||
embed.Title = fmt.Sprintf("%s action completed", action) | ||
embed.Color = 6749952 | ||
case "failed": | ||
embed.Title = fmt.Sprintf("%s action failed", action) | ||
embed.Color = 16724736 | ||
default: | ||
embed.Title = fmt.Sprintf("%s action in progress", action) | ||
embed.Color = 3381759 | ||
} | ||
|
||
return Message{ | ||
Embeds: []*Embed{embed}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package discord | ||
|
||
import ( | ||
"net/http" | ||
"encoding/json" | ||
"fmt" | ||
"bytes" | ||
|
||
log "github.com/sirupsen/logrus" | ||
) | ||
|
||
func PostActionMessage(callbackID string, data map[string]interface{}) error { | ||
msg := newMessage(callbackID, data["action"].(string), data["result"].(string), data["fields"].([]interface{})) | ||
payload, err := json.Marshal(msg) | ||
if err != nil { | ||
log.Errorf("cannot marshal discord message - %v", err) | ||
return err | ||
} | ||
log.Infof("Posting chat message to discord: %+v", msg) | ||
resp, err := http.Post(postMessageHookURL, "application/json", bytes.NewBuffer(payload)) | ||
resp.Body.Close() | ||
if err != nil { | ||
return fmt.Errorf("discord POST request failed - %v", err) | ||
} | ||
log.Infof("Message posted to discord") | ||
return nil | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters