-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Paz
committed
Jan 2, 2025
1 parent
df4f7a2
commit a9ff956
Showing
7 changed files
with
265 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
package matrix | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"errors" | ||
"fmt" | ||
"net/http" | ||
"time" | ||
|
||
"github.com/pazifical/onyx/logging" | ||
) | ||
|
||
type Service struct { | ||
credentials Credentials | ||
roomID string | ||
accessToken string | ||
} | ||
|
||
func NewService(credentials Credentials, roomID string) Service { | ||
return Service{ | ||
credentials: credentials, | ||
roomID: roomID, | ||
} | ||
} | ||
|
||
func (s *Service) Authenticate() error { | ||
endpoint := "https://matrix.org/_matrix/client/v3/login" | ||
|
||
loginRequestData := LoginRequestData{ | ||
Type: "m.login.password", | ||
Password: s.credentials.Password, | ||
Identifier: UserData{ | ||
Type: "m.id.user", | ||
User: s.credentials.Username, | ||
}, | ||
} | ||
|
||
data, err := json.Marshal(loginRequestData) | ||
if err != nil { | ||
logging.Error(err.Error()) | ||
return err | ||
} | ||
|
||
request, err := http.NewRequest("POST", endpoint, bytes.NewBuffer(data)) | ||
if err != nil { | ||
logging.Error(err.Error()) | ||
return err | ||
} | ||
|
||
request.Header.Set("Content-Type", "application/json") | ||
|
||
client := http.Client{ | ||
Timeout: 10 * time.Second, | ||
} | ||
|
||
response, err := client.Do(request) | ||
if err != nil { | ||
logging.Error(err.Error()) | ||
return err | ||
} | ||
|
||
defer response.Body.Close() | ||
if response.StatusCode != http.StatusOK { | ||
logging.Warning(fmt.Sprintf("response status %d from Matrix", response.StatusCode)) | ||
|
||
var errorData interface{} | ||
err = json.NewDecoder(response.Body).Decode(&errorData) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
jsonData, err := json.Marshal(errorData) | ||
if err != nil { | ||
return err | ||
} | ||
return errors.New(string(jsonData)) | ||
} | ||
|
||
var loginResponseData LoginResponseData | ||
err = json.NewDecoder(response.Body).Decode(&loginResponseData) | ||
if err != nil { | ||
logging.Error(err.Error()) | ||
return err | ||
} | ||
|
||
s.accessToken = loginResponseData.AccessToken | ||
|
||
return nil | ||
} | ||
|
||
func (s *Service) SendMessage(text string) error { | ||
endpoint := fmt.Sprintf("https://matrix.org/_matrix/client/v3/rooms/%s/send/m.room.message", s.roomID) | ||
|
||
message := MessageData{ | ||
MsgType: "m.text", | ||
Body: text, | ||
} | ||
|
||
data, err := json.Marshal(message) | ||
if err != nil { | ||
logging.Error(err.Error()) | ||
return err | ||
} | ||
|
||
request, err := http.NewRequest("POST", endpoint, bytes.NewBuffer(data)) | ||
if err != nil { | ||
logging.Error(err.Error()) | ||
return err | ||
} | ||
|
||
request.Header.Set("Content-Type", "application/json") | ||
request.Header.Set("Authorization", fmt.Sprintf("Bearer %s", s.accessToken)) | ||
|
||
client := http.Client{ | ||
Timeout: 10 * time.Second, | ||
} | ||
|
||
response, err := client.Do(request) | ||
if err != nil { | ||
logging.Error(err.Error()) | ||
return err | ||
} | ||
|
||
defer response.Body.Close() | ||
if response.StatusCode != http.StatusOK { | ||
logging.Warning(fmt.Sprintf("response status %d from Matrix", response.StatusCode)) | ||
|
||
var errorData interface{} | ||
err = json.NewDecoder(response.Body).Decode(&errorData) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
jsonData, err := json.Marshal(errorData) | ||
if err != nil { | ||
return err | ||
} | ||
return errors.New(string(jsonData)) | ||
} | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package matrix | ||
|
||
type Credentials struct { | ||
Username string | ||
Password string | ||
} | ||
|
||
type LoginRequestData struct { | ||
Type string `json:"type"` | ||
Identifier UserData `json:"identifier"` | ||
Password string `json:"password"` | ||
} | ||
|
||
type LoginResponseData struct { | ||
AccessToken string `json:"access_token"` | ||
DeviceID string `json:"device_id"` | ||
ExpiresInMs int `json:"expires_in_ms"` | ||
RefreshToken string `json:"refresh_token"` | ||
UserID string `json:"user_id"` | ||
} | ||
|
||
type UserData struct { | ||
Type string `json:"type"` | ||
User string `json:"user"` | ||
} | ||
|
||
type MessageData struct { | ||
MsgType string `json:"msgtype"` | ||
Body string `json:"body"` | ||
} |
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
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 |
---|---|---|
@@ -1 +1 @@ | ||
ONYX_VERSION=0.2.3 | ||
ONYX_VERSION=0.2.4 |