mirror of
https://github.com/hrfee/jfa-go.git
synced 2024-12-22 17:10:10 +00:00
Matrix: Notifications
This commit is contained in:
parent
9bd6abadf4
commit
89fb3fa619
9
email.go
9
email.go
@ -25,6 +25,8 @@ import (
|
|||||||
"github.com/mailgun/mailgun-go/v4"
|
"github.com/mailgun/mailgun-go/v4"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var renderer = html.NewRenderer(html.RendererOptions{Flags: html.Smartypants})
|
||||||
|
|
||||||
// implements email sending, right now via smtp or mailgun.
|
// implements email sending, right now via smtp or mailgun.
|
||||||
type EmailClient interface {
|
type EmailClient interface {
|
||||||
Send(fromName, fromAddr string, message *Message, address ...string) error
|
Send(fromName, fromAddr string, message *Message, address ...string) error
|
||||||
@ -337,7 +339,6 @@ func (emailer *Emailer) constructConfirmation(code, username, key string, app *a
|
|||||||
|
|
||||||
func (emailer *Emailer) constructTemplate(subject, md string, app *appContext) (*Message, error) {
|
func (emailer *Emailer) constructTemplate(subject, md string, app *appContext) (*Message, error) {
|
||||||
email := &Message{Subject: subject}
|
email := &Message{Subject: subject}
|
||||||
renderer := html.NewRenderer(html.RendererOptions{Flags: html.Smartypants})
|
|
||||||
html := markdown.ToHTML([]byte(md), nil, renderer)
|
html := markdown.ToHTML([]byte(md), nil, renderer)
|
||||||
text := stripMarkdown(md)
|
text := stripMarkdown(md)
|
||||||
message := app.config.Section("messages").Key("message").String()
|
message := app.config.Section("messages").Key("message").String()
|
||||||
@ -817,6 +818,12 @@ func (app *appContext) sendByID(email *Message, ID ...string) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if mxChat, ok := app.storage.matrix[id]; ok && mxChat.Contact && matrixEnabled {
|
||||||
|
err = app.matrix.Send(email, mxChat.RoomID)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
if address, ok := app.storage.emails[id]; ok && address.Contact && emailEnabled {
|
if address, ok := app.storage.emails[id]; ok && address.Contact && emailEnabled {
|
||||||
err = app.email.send(email, address.Addr)
|
err = app.email.send(email, address.Addr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
71
matrix.go
71
matrix.go
@ -2,7 +2,10 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/gomarkdown/markdown"
|
||||||
"github.com/matrix-org/gomatrix"
|
"github.com/matrix-org/gomatrix"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -93,7 +96,54 @@ func (d *MatrixDaemon) Shutdown() {
|
|||||||
close(d.ShutdownChannel)
|
close(d.ShutdownChannel)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *MatrixDaemon) handleMessage(event *gomatrix.Event) { return }
|
func (d *MatrixDaemon) handleMessage(event *gomatrix.Event) {
|
||||||
|
if event.Sender == d.userID {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
lang := "en-us"
|
||||||
|
if l, ok := d.languages[event.RoomID]; ok {
|
||||||
|
if _, ok := d.app.storage.lang.Telegram[l]; ok {
|
||||||
|
lang = l
|
||||||
|
}
|
||||||
|
}
|
||||||
|
sects := strings.Split(event.Content["body"].(string), " ")
|
||||||
|
switch sects[0] {
|
||||||
|
case "!lang":
|
||||||
|
if len(sects) == 2 {
|
||||||
|
d.commandLang(event, sects[1], lang)
|
||||||
|
} else {
|
||||||
|
d.commandLang(event, "", lang)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *MatrixDaemon) commandLang(event *gomatrix.Event, code, lang string) {
|
||||||
|
if code == "" {
|
||||||
|
list := "!lang <lang>\n"
|
||||||
|
for c := range d.app.storage.lang.Telegram {
|
||||||
|
list += fmt.Sprintf("%s: %s\n", c, d.app.storage.lang.Telegram[c].Meta.Name)
|
||||||
|
}
|
||||||
|
_, err := d.bot.SendText(
|
||||||
|
event.RoomID,
|
||||||
|
list,
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
d.app.err.Printf("Matrix: Failed to send message to \"%s\": %v", event.Sender, err)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if _, ok := d.app.storage.lang.Telegram[code]; !ok {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
d.languages[event.RoomID] = code
|
||||||
|
if u, ok := d.app.storage.matrix[event.RoomID]; ok {
|
||||||
|
u.Lang = code
|
||||||
|
d.app.storage.matrix[event.RoomID] = u
|
||||||
|
if err := d.app.storage.storeMatrixUsers(); err != nil {
|
||||||
|
d.app.err.Printf("Matrix: Failed to store Matrix users: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func (d *MatrixDaemon) SendStart(userID string) (ok bool) {
|
func (d *MatrixDaemon) SendStart(userID string) (ok bool) {
|
||||||
room, err := d.bot.CreateRoom(&gomatrix.ReqCreateRoom{
|
room, err := d.bot.CreateRoom(&gomatrix.ReqCreateRoom{
|
||||||
@ -128,6 +178,25 @@ func (d *MatrixDaemon) SendStart(userID string) (ok bool) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (d *MatrixDaemon) Send(message *Message, roomID ...string) (err error) {
|
||||||
|
md := ""
|
||||||
|
if message.Markdown != "" {
|
||||||
|
// Convert images to links
|
||||||
|
md = string(markdown.ToHTML([]byte(strings.ReplaceAll(message.Markdown, "![", "[")), nil, renderer))
|
||||||
|
}
|
||||||
|
for _, id := range roomID {
|
||||||
|
if md != "" {
|
||||||
|
_, err = d.bot.SendFormattedText(id, message.Text, md)
|
||||||
|
} else {
|
||||||
|
_, err = d.bot.SendText(id, message.Text)
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
// User enters ID on sign-up, a PIN is sent to them. They enter it on sign-up.
|
// User enters ID on sign-up, a PIN is sent to them. They enter it on sign-up.
|
||||||
|
|
||||||
// Message the user first, to avoid E2EE by default
|
// Message the user first, to avoid E2EE by default
|
||||||
|
Loading…
Reference in New Issue
Block a user