mirror of
https://github.com/tulir/mautrix-whatsapp
synced 2024-12-14 01:14:29 +01:00
Set name for status broadcast room when creating and reformat example config
Fixes #27
This commit is contained in:
parent
67a041c06d
commit
c88e299348
5 changed files with 113 additions and 82 deletions
|
@ -274,7 +274,7 @@ func (handler *CommandHandler) CommandPM(ce *CommandEvent) {
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
err := portal.CreateMatrixRoom([]string{user.MXID})
|
err := portal.CreateMatrixRoom(user)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ce.Reply(fmt.Sprintf("Failed to create portal room: %v", err))
|
ce.Reply(fmt.Sprintf("Failed to create portal room: %v", err))
|
||||||
return
|
return
|
||||||
|
|
|
@ -1,85 +1,86 @@
|
||||||
# Homeserver details.
|
# Homeserver details.
|
||||||
homeserver:
|
homeserver:
|
||||||
# The address that this appservice can use to connect to the homeserver.
|
# The address that this appservice can use to connect to the homeserver.
|
||||||
address: https://matrix.org
|
address: https://matrix.org
|
||||||
# The domain of the homeserver (for MXIDs, etc).
|
# The domain of the homeserver (for MXIDs, etc).
|
||||||
domain: matrix.org
|
domain: matrix.org
|
||||||
|
|
||||||
# Application service host/registration related details.
|
# Application service host/registration related details.
|
||||||
# Changing these values requires regeneration of the registration.
|
# Changing these values requires regeneration of the registration.
|
||||||
appservice:
|
appservice:
|
||||||
# The address that the homeserver can use to connect to this appservice.
|
# The address that the homeserver can use to connect to this appservice.
|
||||||
address: http://localhost:8080
|
address: http://localhost:8080
|
||||||
|
|
||||||
# The hostname and port where this appservice should listen.
|
# The hostname and port where this appservice should listen.
|
||||||
hostname: 0.0.0.0
|
hostname: 0.0.0.0
|
||||||
port: 8080
|
port: 8080
|
||||||
|
|
||||||
# Database config.
|
# Database config.
|
||||||
database:
|
database:
|
||||||
# The database type. Only "sqlite3" is supported.
|
# The database type. "sqlite3" and "postgres" are supported.
|
||||||
type: sqlite3
|
type: sqlite3
|
||||||
# The database URI. Usually file name. https://github.com/mattn/go-sqlite3#connection-string
|
# The database URI.
|
||||||
# postres example: postgres://synapse:changeme@db/whatsapp?sslmode=disable
|
# SQLite: File name is enough. https://github.com/mattn/go-sqlite3#connection-string
|
||||||
uri: mautrix-whatsapp.db
|
# Postgres: Connection string. For example, postgres://user:password@host/database
|
||||||
# Path to the Matrix room state store.
|
uri: mautrix-whatsapp.db
|
||||||
state_store_path: ./mx-state.json
|
# Path to the Matrix room state store.
|
||||||
|
state_store_path: ./mx-state.json
|
||||||
|
|
||||||
# The unique ID of this appservice.
|
# The unique ID of this appservice.
|
||||||
id: whatsapp
|
id: whatsapp
|
||||||
# Appservice bot details.
|
# Appservice bot details.
|
||||||
bot:
|
bot:
|
||||||
# Username of the appservice bot.
|
# Username of the appservice bot.
|
||||||
username: whatsappbot
|
username: whatsappbot
|
||||||
# Display name and avatar for bot. Set to "remove" to remove display name/avatar, leave empty
|
# Display name and avatar for bot. Set to "remove" to remove display name/avatar, leave empty
|
||||||
# to leave display name/avatar as-is.
|
# to leave display name/avatar as-is.
|
||||||
displayname: WhatsApp bridge bot
|
displayname: WhatsApp bridge bot
|
||||||
avatar: mxc://maunium.net/NeXNQarUbrlYBiPCpprYsRqr
|
avatar: mxc://maunium.net/NeXNQarUbrlYBiPCpprYsRqr
|
||||||
|
|
||||||
# Authentication tokens for AS <-> HS communication. Autogenerated; do not modify.
|
# Authentication tokens for AS <-> HS communication. Autogenerated; do not modify.
|
||||||
as_token: "This value is generated when generating the registration"
|
as_token: "This value is generated when generating the registration"
|
||||||
hs_token: "This value is generated when generating the registration"
|
hs_token: "This value is generated when generating the registration"
|
||||||
|
|
||||||
# Bridge config. Currently unused.
|
# Bridge config. Currently unused.
|
||||||
bridge:
|
bridge:
|
||||||
# Localpart template of MXIDs for WhatsApp users.
|
# Localpart template of MXIDs for WhatsApp users.
|
||||||
# {{.}} is replaced with the phone number of the WhatsApp user.
|
# {{.}} is replaced with the phone number of the WhatsApp user.
|
||||||
username_template: whatsapp_{{.}}
|
username_template: whatsapp_{{.}}
|
||||||
# Displayname template for WhatsApp users.
|
# Displayname template for WhatsApp users.
|
||||||
# {{.Notify}} - nickname set by the WhatsApp user
|
# {{.Notify}} - nickname set by the WhatsApp user
|
||||||
# {{.Jid}} - phone number (international format)
|
# {{.Jid}} - phone number (international format)
|
||||||
# The following variables are also available, but will cause problems on multi-user instances:
|
# The following variables are also available, but will cause problems on multi-user instances:
|
||||||
# {{.Name}} - display name from contact list
|
# {{.Name}} - display name from contact list
|
||||||
# {{.Short}} - short display name from contact list
|
# {{.Short}} - short display name from contact list
|
||||||
displayname_template: "{{if .Notify}}{{.Notify}}{{else}}{{.Jid}}{{end}} (WA)"
|
displayname_template: "{{if .Notify}}{{.Notify}}{{else}}{{.Jid}}{{end}} (WA)"
|
||||||
|
|
||||||
# The prefix for commands. Only required in non-management rooms.
|
# The prefix for commands. Only required in non-management rooms.
|
||||||
command_prefix: "!wa"
|
command_prefix: "!wa"
|
||||||
|
|
||||||
# Permissions for using the bridge.
|
# Permissions for using the bridge.
|
||||||
# Permitted values:
|
# Permitted values:
|
||||||
# user - Access to use the bridge to chat with a WhatsApp account.
|
# user - Access to use the bridge to chat with a WhatsApp account.
|
||||||
# admin - User level and some additional administration tools
|
# admin - User level and some additional administration tools
|
||||||
# Permitted keys:
|
# Permitted keys:
|
||||||
# * - All Matrix users
|
# * - All Matrix users
|
||||||
# domain - All users on that homeserver
|
# domain - All users on that homeserver
|
||||||
# mxid - Specific user
|
# mxid - Specific user
|
||||||
permissions:
|
permissions:
|
||||||
"example.com": user
|
"example.com": user
|
||||||
"@admin:example.com": admin
|
"@admin:example.com": admin
|
||||||
|
|
||||||
# Logging config.
|
# Logging config.
|
||||||
logging:
|
logging:
|
||||||
# The directory for log files. Will be created if not found.
|
# The directory for log files. Will be created if not found.
|
||||||
directory: ./logs
|
directory: ./logs
|
||||||
# Available variables: .Date for the file date and .Index for different log files on the same day.
|
# Available variables: .Date for the file date and .Index for different log files on the same day.
|
||||||
file_name_format: "{{.Date}}-{{.Index}}.log"
|
file_name_format: "{{.Date}}-{{.Index}}.log"
|
||||||
# Date format for file names in the Go time format: https://golang.org/pkg/time/#pkg-constants
|
# Date format for file names in the Go time format: https://golang.org/pkg/time/#pkg-constants
|
||||||
file_date_format: 2006-01-02
|
file_date_format: 2006-01-02
|
||||||
# Log file permissions.
|
# Log file permissions.
|
||||||
file_mode: 0600
|
file_mode: 0600
|
||||||
# Timestamp format for log entries in the Go time format.
|
# Timestamp format for log entries in the Go time format.
|
||||||
timestamp_format: Jan _2, 2006 15:04:05
|
timestamp_format: Jan _2, 2006 15:04:05
|
||||||
# Minimum severity for log messages.
|
# Minimum severity for log messages.
|
||||||
# Options: debug, info, warn, error, fatal
|
# Options: debug, info, warn, error, fatal
|
||||||
print_level: debug
|
print_level: debug
|
||||||
|
|
1
go.mod
1
go.mod
|
@ -4,6 +4,7 @@ require (
|
||||||
github.com/Baozisoftware/qrcode-terminal-go v0.0.0-20170407111555-c0650d8dff0f // indirect
|
github.com/Baozisoftware/qrcode-terminal-go v0.0.0-20170407111555-c0650d8dff0f // indirect
|
||||||
github.com/Rhymen/go-whatsapp v0.0.0-20190208090600-c1173899de99
|
github.com/Rhymen/go-whatsapp v0.0.0-20190208090600-c1173899de99
|
||||||
github.com/gorilla/mux v1.7.0 // indirect
|
github.com/gorilla/mux v1.7.0 // indirect
|
||||||
|
github.com/lib/pq v1.0.0
|
||||||
github.com/mattn/go-colorable v0.1.1 // indirect
|
github.com/mattn/go-colorable v0.1.1 // indirect
|
||||||
github.com/mattn/go-sqlite3 v1.10.0
|
github.com/mattn/go-sqlite3 v1.10.0
|
||||||
github.com/shurcooL/sanitized_anchor_name v1.0.0 // indirect
|
github.com/shurcooL/sanitized_anchor_name v1.0.0 // indirect
|
||||||
|
|
2
go.sum
2
go.sum
|
@ -10,6 +10,8 @@ github.com/gorilla/mux v1.7.0 h1:tOSd0UKHQd6urX6ApfOn4XdBMY6Sh1MfxV3kmaazO+U=
|
||||||
github.com/gorilla/mux v1.7.0/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs=
|
github.com/gorilla/mux v1.7.0/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs=
|
||||||
github.com/gorilla/websocket v1.4.0 h1:WDFjx/TMzVgy9VdMMQi2K2Emtwi2QcUQsztZ/zLaH/Q=
|
github.com/gorilla/websocket v1.4.0 h1:WDFjx/TMzVgy9VdMMQi2K2Emtwi2QcUQsztZ/zLaH/Q=
|
||||||
github.com/gorilla/websocket v1.4.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ=
|
github.com/gorilla/websocket v1.4.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ=
|
||||||
|
github.com/lib/pq v1.0.0 h1:X5PMW56eZitiTeO7tKzZxFCSpbFZJtkMMooicw2us9A=
|
||||||
|
github.com/lib/pq v1.0.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
|
||||||
github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU=
|
github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU=
|
||||||
github.com/mattn/go-colorable v0.1.1 h1:G1f5SKeVxmagw/IyvzvtZE4Gybcc4Tr1tf7I8z0XgOg=
|
github.com/mattn/go-colorable v0.1.1 h1:G1f5SKeVxmagw/IyvzvtZE4Gybcc4Tr1tf7I8z0XgOg=
|
||||||
github.com/mattn/go-colorable v0.1.1/go.mod h1:FuOcm+DKB9mbwrcAfNl7/TZVBZ6rcnceauSikq3lYCQ=
|
github.com/mattn/go-colorable v0.1.1/go.mod h1:FuOcm+DKB9mbwrcAfNl7/TZVBZ6rcnceauSikq3lYCQ=
|
||||||
|
|
55
portal.go
55
portal.go
|
@ -300,7 +300,10 @@ func (portal *Portal) UpdateAvatar(user *User, avatar *whatsappExt.ProfilePicInf
|
||||||
|
|
||||||
func (portal *Portal) UpdateName(name string, setBy types.WhatsAppID) bool {
|
func (portal *Portal) UpdateName(name string, setBy types.WhatsAppID) bool {
|
||||||
if portal.Name != name {
|
if portal.Name != name {
|
||||||
intent := portal.bridge.GetPuppetByJID(setBy).Intent()
|
intent := portal.MainIntent()
|
||||||
|
if len(setBy) > 0 {
|
||||||
|
intent = portal.bridge.GetPuppetByJID(setBy).Intent()
|
||||||
|
}
|
||||||
_, err := intent.SetRoomName(portal.MXID, name)
|
_, err := intent.SetRoomName(portal.MXID, name)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
portal.Name = name
|
portal.Name = name
|
||||||
|
@ -313,7 +316,10 @@ func (portal *Portal) UpdateName(name string, setBy types.WhatsAppID) bool {
|
||||||
|
|
||||||
func (portal *Portal) UpdateTopic(topic string, setBy types.WhatsAppID) bool {
|
func (portal *Portal) UpdateTopic(topic string, setBy types.WhatsAppID) bool {
|
||||||
if portal.Topic != topic {
|
if portal.Topic != topic {
|
||||||
intent := portal.bridge.GetPuppetByJID(setBy).Intent()
|
intent := portal.MainIntent()
|
||||||
|
if len(setBy) > 0 {
|
||||||
|
intent = portal.bridge.GetPuppetByJID(setBy).Intent()
|
||||||
|
}
|
||||||
_, err := intent.SetRoomTopic(portal.MXID, topic)
|
_, err := intent.SetRoomTopic(portal.MXID, topic)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
portal.Topic = topic
|
portal.Topic = topic
|
||||||
|
@ -325,6 +331,14 @@ func (portal *Portal) UpdateTopic(topic string, setBy types.WhatsAppID) bool {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (portal *Portal) UpdateMetadata(user *User) bool {
|
func (portal *Portal) UpdateMetadata(user *User) bool {
|
||||||
|
if portal.IsPrivateChat() {
|
||||||
|
return false
|
||||||
|
} else if portal.IsStatusBroadcastRoom() {
|
||||||
|
update := false
|
||||||
|
update = portal.UpdateName("WhatsApp Status Broadcast", "") || update
|
||||||
|
update = portal.UpdateTopic("WhatsApp status updates from your contacts", "") || update
|
||||||
|
return update
|
||||||
|
}
|
||||||
metadata, err := user.Conn.GetGroupMetaData(portal.Key.JID)
|
metadata, err := user.Conn.GetGroupMetaData(portal.Key.JID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
portal.log.Errorln(err)
|
portal.log.Errorln(err)
|
||||||
|
@ -354,7 +368,7 @@ func (portal *Portal) Sync(user *User, contact whatsapp.Contact) {
|
||||||
|
|
||||||
if len(portal.MXID) == 0 {
|
if len(portal.MXID) == 0 {
|
||||||
portal.Name = contact.Name
|
portal.Name = contact.Name
|
||||||
err := portal.CreateMatrixRoom([]string{user.MXID})
|
err := portal.CreateMatrixRoom(user)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
portal.log.Errorln("Failed to create portal room:", err)
|
portal.log.Errorln("Failed to create portal room:", err)
|
||||||
return
|
return
|
||||||
|
@ -368,7 +382,9 @@ func (portal *Portal) Sync(user *User, contact whatsapp.Contact) {
|
||||||
|
|
||||||
update := false
|
update := false
|
||||||
update = portal.UpdateMetadata(user) || update
|
update = portal.UpdateMetadata(user) || update
|
||||||
update = portal.UpdateAvatar(user, nil) || update
|
if !portal.IsStatusBroadcastRoom() {
|
||||||
|
update = portal.UpdateAvatar(user, nil) || update
|
||||||
|
}
|
||||||
if update {
|
if update {
|
||||||
portal.Update()
|
portal.Update()
|
||||||
}
|
}
|
||||||
|
@ -459,7 +475,7 @@ func (portal *Portal) RestrictMetadataChanges(restrict bool) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (portal *Portal) CreateMatrixRoom(invite []string) error {
|
func (portal *Portal) CreateMatrixRoom(user *User) error {
|
||||||
portal.roomCreateLock.Lock()
|
portal.roomCreateLock.Lock()
|
||||||
defer portal.roomCreateLock.Unlock()
|
defer portal.roomCreateLock.Unlock()
|
||||||
if len(portal.MXID) > 0 {
|
if len(portal.MXID) > 0 {
|
||||||
|
@ -471,20 +487,27 @@ func (portal *Portal) CreateMatrixRoom(invite []string) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
name := portal.Name
|
|
||||||
topic := portal.Topic
|
|
||||||
isPrivateChat := false
|
isPrivateChat := false
|
||||||
if portal.IsPrivateChat() {
|
if portal.IsPrivateChat() {
|
||||||
name = ""
|
portal.Name = ""
|
||||||
topic = "WhatsApp private chat"
|
portal.Topic = "WhatsApp private chat"
|
||||||
isPrivateChat = true
|
isPrivateChat = true
|
||||||
|
} else if portal.IsStatusBroadcastRoom() {
|
||||||
|
portal.Name = "WhatsApp Status Broadcast"
|
||||||
|
portal.Topic = "WhatsApp status updates from your contacts"
|
||||||
|
} else {
|
||||||
|
metadata, err := user.Conn.GetGroupMetaData(portal.Key.JID)
|
||||||
|
if err == nil && metadata.Status == 0 {
|
||||||
|
portal.Name = metadata.Name
|
||||||
|
portal.Topic = metadata.Topic
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
resp, err := intent.CreateRoom(&mautrix.ReqCreateRoom{
|
resp, err := intent.CreateRoom(&mautrix.ReqCreateRoom{
|
||||||
Visibility: "private",
|
Visibility: "private",
|
||||||
Name: name,
|
Name: portal.Name,
|
||||||
Topic: topic,
|
Topic: portal.Topic,
|
||||||
Invite: invite,
|
Invite: []string{user.MXID},
|
||||||
Preset: "private_chat",
|
Preset: "private_chat",
|
||||||
IsDirect: isPrivateChat,
|
IsDirect: isPrivateChat,
|
||||||
InitialState: []*mautrix.Event{{
|
InitialState: []*mautrix.Event{{
|
||||||
|
@ -510,6 +533,10 @@ func (portal *Portal) IsPrivateChat() bool {
|
||||||
return *portal.isPrivate
|
return *portal.isPrivate
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (portal *Portal) IsStatusBroadcastRoom() bool {
|
||||||
|
return portal.Key.JID == "status@broadcast"
|
||||||
|
}
|
||||||
|
|
||||||
func (portal *Portal) MainIntent() *appservice.IntentAPI {
|
func (portal *Portal) MainIntent() *appservice.IntentAPI {
|
||||||
if portal.IsPrivateChat() {
|
if portal.IsPrivateChat() {
|
||||||
return portal.bridge.GetPuppetByJID(portal.Key.JID).Intent()
|
return portal.bridge.GetPuppetByJID(portal.Key.JID).Intent()
|
||||||
|
@ -560,7 +587,7 @@ func (portal *Portal) HandleTextMessage(source *User, message whatsapp.TextMessa
|
||||||
}
|
}
|
||||||
defer lock.Unlock()
|
defer lock.Unlock()
|
||||||
|
|
||||||
err := portal.CreateMatrixRoom([]string{source.MXID})
|
err := portal.CreateMatrixRoom(source)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
portal.log.Errorln("Failed to create portal room:", err)
|
portal.log.Errorln("Failed to create portal room:", err)
|
||||||
return
|
return
|
||||||
|
@ -595,7 +622,7 @@ func (portal *Portal) HandleMediaMessage(source *User, download func() ([]byte,
|
||||||
}
|
}
|
||||||
defer lock.Unlock()
|
defer lock.Unlock()
|
||||||
|
|
||||||
err := portal.CreateMatrixRoom([]string{source.MXID})
|
err := portal.CreateMatrixRoom(source)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
portal.log.Errorln("Failed to create portal room:", err)
|
portal.log.Errorln("Failed to create portal room:", err)
|
||||||
return
|
return
|
||||||
|
|
Loading…
Reference in a new issue