mirror of
https://github.com/pushbits/server.git
synced 2025-07-23 19:37:52 +02:00
send first message via mautrix
This commit is contained in:
parent
0ee3f69d15
commit
e48cbae59b
4 changed files with 101 additions and 11 deletions
|
@ -5,6 +5,8 @@ import (
|
|||
|
||||
"github.com/matrix-org/gomatrix"
|
||||
"github.com/pushbits/server/internal/configuration"
|
||||
"maunium.net/go/mautrix"
|
||||
"maunium.net/go/mautrix/id"
|
||||
)
|
||||
|
||||
var (
|
||||
|
@ -13,14 +15,16 @@ var (
|
|||
|
||||
// Dispatcher holds information for sending notifications to clients.
|
||||
type Dispatcher struct {
|
||||
client *gomatrix.Client
|
||||
formatting configuration.Formatting
|
||||
client *gomatrix.Client // TODO get rid of this client as a dependency
|
||||
mautrixClient *mautrix.Client
|
||||
formatting configuration.Formatting
|
||||
}
|
||||
|
||||
// Create instanciates a dispatcher connection.
|
||||
func Create(homeserver, username, password string, formatting configuration.Formatting) (*Dispatcher, error) {
|
||||
log.Println("Setting up dispatcher.")
|
||||
|
||||
// TODO remove from here
|
||||
client, err := gomatrix.NewClient(homeserver, "", "")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
@ -36,8 +40,25 @@ func Create(homeserver, username, password string, formatting configuration.Form
|
|||
}
|
||||
|
||||
client.SetCredentials(response.UserID, response.AccessToken)
|
||||
// To here
|
||||
|
||||
return &Dispatcher{client: client, formatting: formatting}, nil
|
||||
matrixClient, err := mautrix.NewClient(homeserver, "", "")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
_, err = matrixClient.Login(&mautrix.ReqLogin{
|
||||
Type: "m.login.password",
|
||||
Identifier: mautrix.UserIdentifier{Type: mautrix.IdentifierTypeUser, User: username},
|
||||
Password: password,
|
||||
DeviceID: id.DeviceID("my-device"), // TODO make device ID configurable
|
||||
StoreCredentials: true,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &Dispatcher{client: client, formatting: formatting, mautrixClient: matrixClient}, nil
|
||||
}
|
||||
|
||||
// Close closes the dispatcher connection.
|
||||
|
|
|
@ -10,6 +10,8 @@ import (
|
|||
"github.com/matrix-org/gomatrix"
|
||||
"github.com/pushbits/server/internal/model"
|
||||
"github.com/pushbits/server/internal/pberrors"
|
||||
"maunium.net/go/mautrix/event"
|
||||
"maunium.net/go/mautrix/id"
|
||||
)
|
||||
|
||||
// MessageFormat is a matrix message format
|
||||
|
@ -50,7 +52,7 @@ type NewContent struct {
|
|||
}
|
||||
|
||||
// SendNotification sends a notification to the specified user.
|
||||
func (d *Dispatcher) SendNotification(a *model.Application, n *model.Notification) (id string, err error) {
|
||||
func (d *Dispatcher) SendNotification(a *model.Application, n *model.Notification) (eventId string, err error) {
|
||||
log.Printf("Sending notification to room %s.", a.MatrixID)
|
||||
|
||||
plainMessage := strings.TrimSpace(n.Message)
|
||||
|
@ -61,9 +63,16 @@ func (d *Dispatcher) SendNotification(a *model.Application, n *model.Notificatio
|
|||
text := fmt.Sprintf("%s\n\n%s", plainTitle, plainMessage)
|
||||
formattedText := fmt.Sprintf("%s %s", title, message)
|
||||
|
||||
respSendEvent, err := d.client.SendFormattedText(a.MatrixID, text, formattedText)
|
||||
messageEvent := &MessageEvent{
|
||||
Body: text,
|
||||
FormattedBody: formattedText,
|
||||
MsgType: MsgTypeText,
|
||||
Format: MessageFormatHTML,
|
||||
}
|
||||
|
||||
return respSendEvent.EventID, err
|
||||
evt, err := d.mautrixClient.SendMessageEvent(id.RoomID(a.MatrixID), event.EventMessage, &messageEvent)
|
||||
|
||||
return evt.EventID.String(), err
|
||||
}
|
||||
|
||||
// DeleteNotification sends a notification to the specified user that another notificaion is deleted
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue