Adding functionality to join rooms.
This commit is contained in:
parent
c21eb8e3c2
commit
ed08994a7c
@ -24,6 +24,10 @@ func Handle(cfg config.Config) http.HandlerFunc {
|
|||||||
log.Error().Err(err).Msg("An error has occurred")
|
log.Error().Err(err).Msg("An error has occurred")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Attempt to join room before sending a message.
|
||||||
|
matrix.JoinRoom(cfg, vars, token)
|
||||||
|
|
||||||
|
// Publish to Matrix
|
||||||
resp := matrix.PublishText(cfg, vars, reqBody, token)
|
resp := matrix.PublishText(cfg, vars, reqBody, token)
|
||||||
|
|
||||||
router.Respond(w, 200, resp)
|
router.Respond(w, 200, resp)
|
||||||
|
@ -29,24 +29,7 @@ func GetToken(cfg config.Config, vars map[string]string) string {
|
|||||||
}
|
}
|
||||||
s := fmt.Sprintf("%v:%v/_matrix/client/r0/login", cfg.Matrix.Homeserver, cfg.Matrix.Port)
|
s := fmt.Sprintf("%v:%v/_matrix/client/r0/login", cfg.Matrix.Homeserver, cfg.Matrix.Port)
|
||||||
|
|
||||||
req, err := http.NewRequest(http.MethodPost, s, bytes.NewBuffer(reqBody))
|
body := postRequest(s, bytes.NewBuffer(reqBody))
|
||||||
if err != nil {
|
|
||||||
log.Error().Err(err).Msg("matrix.GetToken.req" + err.Error())
|
|
||||||
}
|
|
||||||
|
|
||||||
client := &http.Client{}
|
|
||||||
|
|
||||||
resp, err := client.Do(req)
|
|
||||||
if err != nil {
|
|
||||||
log.Error().Err(err).Msg("matrix.GetToken.resp" + err.Error())
|
|
||||||
}
|
|
||||||
|
|
||||||
defer resp.Body.Close()
|
|
||||||
|
|
||||||
body, err := ioutil.ReadAll(resp.Body)
|
|
||||||
if err != nil {
|
|
||||||
log.Error().Err(err).Msg("matrix.GetToken.body" + err.Error())
|
|
||||||
}
|
|
||||||
|
|
||||||
respBody := struct {
|
respBody := struct {
|
||||||
AccessToken string `json:"access_token"`
|
AccessToken string `json:"access_token"`
|
||||||
@ -74,18 +57,32 @@ func PublishText(cfg config.Config, vars map[string]string, data []byte, token s
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal().Err(err).Msg(err.Error())
|
log.Fatal().Err(err).Msg(err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
s := fmt.Sprintf("%v:%v/_matrix/client/r0/rooms/%v/send/m.room.message?access_token=%v", cfg.Matrix.Homeserver, cfg.Matrix.Port, vars["matrixRoom"], token)
|
s := fmt.Sprintf("%v:%v/_matrix/client/r0/rooms/%v/send/m.room.message?access_token=%v", cfg.Matrix.Homeserver, cfg.Matrix.Port, vars["matrixRoom"], token)
|
||||||
|
|
||||||
req, err := http.NewRequest(http.MethodPost, s, bytes.NewBuffer(reqBody))
|
body := postRequest(s, bytes.NewBuffer(reqBody))
|
||||||
|
|
||||||
|
return body
|
||||||
|
}
|
||||||
|
|
||||||
|
// JoinRoom will attempt to join a matrix rooom, assuming there is an invite pending.
|
||||||
|
func JoinRoom(cfg config.Config, vars map[string]string, token string) {
|
||||||
|
s := fmt.Sprintf("%v:%v/_matrix/client/r0/rooms/%v/join?access_token=%v", cfg.Matrix.Homeserver, cfg.Matrix.Port, vars["matrixRoom"], token)
|
||||||
|
_ = postRequest(s, bytes.NewBuffer(nil))
|
||||||
|
}
|
||||||
|
|
||||||
|
// postRequest performs the post requests to the Matrix server.
|
||||||
|
func postRequest(s string, data *bytes.Buffer) []byte {
|
||||||
|
req, err := http.NewRequest(http.MethodPost, s, data)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error().Err(err).Msg("matrix.PublishText.req" + err.Error())
|
log.Error().Err(err).Msg("matrix.postRequest.req" + err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
client := &http.Client{}
|
client := &http.Client{}
|
||||||
|
|
||||||
resp, err := client.Do(req)
|
resp, err := client.Do(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error().Err(err).Msg("matrix.PublishText.resp" + err.Error())
|
log.Error().Err(err).Msg("matrix.postRequest.resp" + err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
body, err := ioutil.ReadAll(resp.Body)
|
body, err := ioutil.ReadAll(resp.Body)
|
||||||
|
@ -17,7 +17,7 @@ import (
|
|||||||
|
|
||||||
// Run ...
|
// Run ...
|
||||||
func Run(info *router.BuildInfo) error {
|
func Run(info *router.BuildInfo) error {
|
||||||
conf, err := config.GetConfig("/config/config.json")
|
conf, err := config.GetConfig("config.json")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal().Err(err)
|
log.Fatal().Err(err)
|
||||||
}
|
}
|
||||||
|
@ -27,6 +27,10 @@ func Handle(cfg config.Config) http.HandlerFunc {
|
|||||||
|
|
||||||
data := parseSlack(reqBody)
|
data := parseSlack(reqBody)
|
||||||
|
|
||||||
|
// Attempt to join romo before publishing
|
||||||
|
matrix.JoinRoom(cfg, vars, token)
|
||||||
|
|
||||||
|
// Publish to Matrix
|
||||||
resp := matrix.PublishText(cfg, vars, []byte(data), token)
|
resp := matrix.PublishText(cfg, vars, []byte(data), token)
|
||||||
|
|
||||||
router.Respond(w, 200, resp)
|
router.Respond(w, 200, resp)
|
||||||
|
Loading…
Reference in New Issue
Block a user