forked from MirrorHub/mautrix-whatsapp
Add simple method for sending bridge notices
This commit is contained in:
parent
1aca93f250
commit
ded9e31315
1 changed files with 32 additions and 27 deletions
59
user.go
59
user.go
|
@ -233,9 +233,8 @@ func (user *User) Connect(evenIfNoSession bool) bool {
|
||||||
conn, err := whatsapp.NewConn(timeout * time.Second)
|
conn, err := whatsapp.NewConn(timeout * time.Second)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
user.log.Errorln("Failed to connect to WhatsApp:", err)
|
user.log.Errorln("Failed to connect to WhatsApp:", err)
|
||||||
msg := format.RenderMarkdown("\u26a0 Failed to connect to WhatsApp server. "+
|
user.sendMarkdownBridgeAlert("\u26a0 Failed to connect to WhatsApp server. " +
|
||||||
"This indicates a network problem on the bridge server. See bridge logs for more info.", true, false)
|
"This indicates a network problem on the bridge server. See bridge logs for more info.")
|
||||||
_, _ = user.bridge.Bot.SendMessageEvent(user.GetManagementRoom(), event.EventMessage, msg)
|
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
user.Conn = whatsappExt.ExtendConn(conn)
|
user.Conn = whatsappExt.ExtendConn(conn)
|
||||||
|
@ -252,9 +251,8 @@ func (user *User) RestoreSession() bool {
|
||||||
return true
|
return true
|
||||||
} else if err != nil {
|
} else if err != nil {
|
||||||
user.log.Errorln("Failed to restore session:", err)
|
user.log.Errorln("Failed to restore session:", err)
|
||||||
msg := format.RenderMarkdown("\u26a0 Failed to connect to WhatsApp. Make sure WhatsApp "+
|
user.sendMarkdownBridgeAlert("\u26a0 Failed to connect to WhatsApp. Make sure WhatsApp " +
|
||||||
"on your phone is reachable and use `reconnect` to try connecting again.", true, false)
|
"on your phone is reachable and use `reconnect` to try connecting again.")
|
||||||
_, _ = user.bridge.Bot.SendMessageEvent(user.GetManagementRoom(), event.EventMessage, msg)
|
|
||||||
user.log.Debugln("Disconnecting due to failed session restore...")
|
user.log.Debugln("Disconnecting due to failed session restore...")
|
||||||
_, err := user.Conn.Disconnect()
|
_, err := user.Conn.Disconnect()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -434,6 +432,23 @@ func (user *User) tryAutomaticDoublePuppeting() {
|
||||||
user.log.Infoln("Successfully automatically enabled custom puppet")
|
user.log.Infoln("Successfully automatically enabled custom puppet")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (user *User) sendBridgeNotice(formatString string, args ...interface{}) {
|
||||||
|
notice := fmt.Sprintf(formatString, args...)
|
||||||
|
_, err := user.bridge.Bot.SendNotice(user.GetManagementRoom(), notice)
|
||||||
|
if err != nil {
|
||||||
|
user.log.Warnf("Failed to send bridge notice \"%s\": %v", notice, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (user *User) sendMarkdownBridgeAlert(formatString string, args ...interface{}) {
|
||||||
|
notice := fmt.Sprintf(formatString, args...)
|
||||||
|
content := format.RenderMarkdown(notice, true, false)
|
||||||
|
_, err := user.bridge.Bot.SendMessageEvent(user.GetManagementRoom(), event.EventMessage, content)
|
||||||
|
if err != nil {
|
||||||
|
user.log.Warnf("Failed to send bridge alert \"%s\": %v", notice, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func (user *User) intPostLogin() {
|
func (user *User) intPostLogin() {
|
||||||
defer user.syncWait.Done()
|
defer user.syncWait.Done()
|
||||||
user.createCommunity()
|
user.createCommunity()
|
||||||
|
@ -583,12 +598,11 @@ func (user *User) HandleError(err error) {
|
||||||
|
|
||||||
func (user *User) tryReconnect(msg string) {
|
func (user *User) tryReconnect(msg string) {
|
||||||
if user.ConnectionErrors > user.bridge.Config.Bridge.MaxConnectionAttempts {
|
if user.ConnectionErrors > user.bridge.Config.Bridge.MaxConnectionAttempts {
|
||||||
content := format.RenderMarkdown(fmt.Sprintf("%s. Use the `reconnect` command to reconnect.", msg), true, false)
|
user.sendMarkdownBridgeAlert("%s. Use the `reconnect` command to reconnect.", msg)
|
||||||
_, _ = user.bridge.Bot.SendMessageEvent(user.GetManagementRoom(), event.EventMessage, content)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if user.bridge.Config.Bridge.ReportConnectionRetry {
|
if user.bridge.Config.Bridge.ReportConnectionRetry {
|
||||||
_, _ = user.bridge.Bot.SendNotice(user.GetManagementRoom(), fmt.Sprintf("%s. Reconnecting...", msg))
|
user.sendBridgeNotice("%s. Reconnecting...", msg)
|
||||||
// Don't want the same error to be repeated
|
// Don't want the same error to be repeated
|
||||||
msg = ""
|
msg = ""
|
||||||
}
|
}
|
||||||
|
@ -605,7 +619,7 @@ func (user *User) tryReconnect(msg string) {
|
||||||
if err == nil {
|
if err == nil {
|
||||||
user.ConnectionErrors = 0
|
user.ConnectionErrors = 0
|
||||||
if user.bridge.Config.Bridge.ReportConnectionRetry {
|
if user.bridge.Config.Bridge.ReportConnectionRetry {
|
||||||
_, _ = user.bridge.Bot.SendNotice(user.GetManagementRoom(), "Reconnected successfully")
|
user.sendBridgeNotice("Reconnected successfully")
|
||||||
}
|
}
|
||||||
user.PostLogin()
|
user.PostLogin()
|
||||||
return
|
return
|
||||||
|
@ -627,22 +641,17 @@ func (user *User) tryReconnect(msg string) {
|
||||||
delay = (1 << tries) + baseDelay
|
delay = (1 << tries) + baseDelay
|
||||||
}
|
}
|
||||||
if user.bridge.Config.Bridge.ReportConnectionRetry {
|
if user.bridge.Config.Bridge.ReportConnectionRetry {
|
||||||
_, _ = user.bridge.Bot.SendNotice(user.GetManagementRoom(),
|
user.sendBridgeNotice("Reconnection attempt failed: %v. Retrying in %d seconds...", err, delay)
|
||||||
fmt.Sprintf("Reconnection attempt failed: %v. Retrying in %d seconds...", err, delay))
|
|
||||||
}
|
}
|
||||||
time.Sleep(delay * time.Second)
|
time.Sleep(delay * time.Second)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if user.bridge.Config.Bridge.ReportConnectionRetry {
|
if user.bridge.Config.Bridge.ReportConnectionRetry {
|
||||||
msg = fmt.Sprintf("%d reconnection attempts failed. Use the `reconnect` command to try to reconnect manually.", tries)
|
user.sendMarkdownBridgeAlert("%d reconnection attempts failed. Use the `reconnect` command to try to reconnect manually.", tries)
|
||||||
} else {
|
} else {
|
||||||
msg = fmt.Sprintf("\u26a0 %sAdditionally, %d reconnection attempts failed. "+
|
user.sendMarkdownBridgeAlert("\u26a0 %s. Additionally, %d reconnection attempts failed. Use the `reconnect` command to try to reconnect.", msg, tries)
|
||||||
"Use the `reconnect` command to try to reconnect.", msg, tries)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
content := format.RenderMarkdown(msg, true, false)
|
|
||||||
_, _ = user.bridge.Bot.SendMessageEvent(user.GetManagementRoom(), event.EventMessage, content)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (user *User) ShouldCallSynchronously() bool {
|
func (user *User) ShouldCallSynchronously() bool {
|
||||||
|
@ -704,9 +713,7 @@ func (user *User) HandleBatteryMessage(battery whatsapp.BatteryMessage) {
|
||||||
user.batteryWarningsSent = 0
|
user.batteryWarningsSent = 0
|
||||||
}
|
}
|
||||||
if notice != "" {
|
if notice != "" {
|
||||||
go func() {
|
go user.sendBridgeNotice("%s", notice)
|
||||||
_, _ = user.bridge.Bot.SendNotice(user.GetManagementRoom(), notice)
|
|
||||||
}()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -850,17 +857,15 @@ func (user *User) HandleCommand(cmd whatsappExt.Command) {
|
||||||
go portal.UpdateAvatar(user, cmd.ProfilePicInfo, true)
|
go portal.UpdateAvatar(user, cmd.ProfilePicInfo, true)
|
||||||
}
|
}
|
||||||
case whatsappExt.CommandDisconnect:
|
case whatsappExt.CommandDisconnect:
|
||||||
var msg string
|
user.cleanDisconnection = true
|
||||||
if cmd.Kind == "replaced" {
|
if cmd.Kind == "replaced" {
|
||||||
msg = "\u26a0 Your WhatsApp connection was closed by the server because you opened another WhatsApp Web client.\n\n" +
|
go user.sendMarkdownBridgeAlert("\u26a0 Your WhatsApp connection was closed by the server because you opened another WhatsApp Web client.\n\n" +
|
||||||
"Use the `reconnect` command to disconnect the other client and resume bridging."
|
"Use the `reconnect` command to disconnect the other client and resume bridging.")
|
||||||
} else {
|
} else {
|
||||||
user.log.Warnln("Unknown kind of disconnect:", string(cmd.Raw))
|
user.log.Warnln("Unknown kind of disconnect:", string(cmd.Raw))
|
||||||
msg = fmt.Sprintf("\u26a0 Your WhatsApp connection was closed by the server (reason code: %s).\n\n"+
|
go user.sendMarkdownBridgeAlert("\u26a0 Your WhatsApp connection was closed by the server (reason code: %s).\n\n"+
|
||||||
"Use the `reconnect` command to reconnect.", cmd.Kind)
|
"Use the `reconnect` command to reconnect.", cmd.Kind)
|
||||||
}
|
}
|
||||||
user.cleanDisconnection = true
|
|
||||||
go user.bridge.Bot.SendMessageEvent(user.GetManagementRoom(), event.EventMessage, format.RenderMarkdown(msg, true, false))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue