2019-02-21 20:28:13 +01:00
package bwhatsapp
import (
2022-03-20 02:20:54 +01:00
"bytes"
"crypto/rand"
"encoding/hex"
2019-02-21 20:28:13 +01:00
"errors"
"fmt"
2020-01-09 18:14:01 +01:00
"mime"
2019-02-21 20:28:13 +01:00
"os"
2020-01-09 18:14:01 +01:00
"path/filepath"
2022-03-20 02:20:54 +01:00
"strings"
2019-02-21 20:28:13 +01:00
"time"
"github.com/42wim/matterbridge/bridge"
"github.com/42wim/matterbridge/bridge/config"
2022-03-20 02:20:54 +01:00
"github.com/Rhymen/go-whatsapp"
2019-02-21 20:28:13 +01:00
)
const (
// Account config parameters
2022-03-20 02:20:54 +01:00
cfgNumber = "Number"
qrOnWhiteTerminal = "QrOnWhiteTerminal"
sessionFile = "SessionFile"
2019-02-21 20:28:13 +01:00
)
// Bwhatsapp Bridge structure keeping all the information needed for relying
type Bwhatsapp struct {
* bridge . Config
2022-03-20 02:20:54 +01:00
session * whatsapp . Session
conn * whatsapp . Conn
startedAt uint64
users map [ string ] whatsapp . Contact
2019-02-21 20:28:13 +01:00
userAvatars map [ string ] string
}
// New Create a new WhatsApp bridge. This will be called for each [whatsapp.<server>] entry you have in the config file
func New ( cfg * bridge . Config ) bridge . Bridger {
number := cfg . GetString ( cfgNumber )
2020-11-27 00:34:44 +01:00
2019-02-21 20:28:13 +01:00
if number == "" {
cfg . Log . Fatalf ( "Missing configuration for WhatsApp bridge: Number" )
}
b := & Bwhatsapp {
Config : cfg ,
2022-03-20 02:20:54 +01:00
users : make ( map [ string ] whatsapp . Contact ) ,
2019-02-21 20:28:13 +01:00
userAvatars : make ( map [ string ] string ) ,
}
2020-11-27 00:34:44 +01:00
2019-02-21 20:28:13 +01:00
return b
}
// Connect to WhatsApp. Required implementation of the Bridger interface
func ( b * Bwhatsapp ) Connect ( ) error {
number := b . GetString ( cfgNumber )
if number == "" {
2020-11-27 00:34:44 +01:00
return errors . New ( "whatsapp's telephone number need to be configured" )
2019-02-21 20:28:13 +01:00
}
b . Log . Debugln ( "Connecting to WhatsApp.." )
2022-03-20 02:20:54 +01:00
conn , err := whatsapp . NewConn ( 20 * time . Second )
if err != nil {
return errors . New ( "failed to connect to WhatsApp: " + err . Error ( ) )
}
2019-02-21 20:28:13 +01:00
2022-03-20 02:20:54 +01:00
b . conn = conn
2019-02-21 20:28:13 +01:00
2022-03-20 02:20:54 +01:00
b . conn . AddHandler ( b )
b . Log . Debugln ( "WhatsApp connection successful" )
2019-02-21 20:28:13 +01:00
2022-03-20 02:20:54 +01:00
// load existing session in order to keep it between restarts
b . session , err = b . restoreSession ( )
2020-11-27 00:34:44 +01:00
if err != nil {
2022-03-20 02:20:54 +01:00
b . Log . Warn ( err . Error ( ) )
2019-02-21 20:28:13 +01:00
}
2022-03-20 02:20:54 +01:00
// login to a new session
if b . session == nil {
if err = b . Login ( ) ; err != nil {
return err
2022-03-13 01:01:42 +01:00
}
}
2022-03-20 02:20:54 +01:00
b . startedAt = uint64 ( time . Now ( ) . Unix ( ) )
2022-03-13 01:01:42 +01:00
2022-03-20 02:20:54 +01:00
_ , err = b . conn . Contacts ( )
if err != nil {
return fmt . Errorf ( "error on update of contacts: %v" , err )
2019-02-21 20:28:13 +01:00
}
2020-11-27 00:34:44 +01:00
2022-03-20 02:20:54 +01:00
// see https://github.com/Rhymen/go-whatsapp/issues/137#issuecomment-480316013
for len ( b . conn . Store . Contacts ) == 0 {
b . conn . Contacts ( ) // nolint:errcheck
2019-02-21 20:28:13 +01:00
2022-03-20 02:20:54 +01:00
<- time . After ( 1 * time . Second )
2019-02-21 20:28:13 +01:00
}
// map all the users
2022-03-20 02:20:54 +01:00
for id , contact := range b . conn . Store . Contacts {
if ! isGroupJid ( id ) && id != "status@broadcast" {
2019-02-21 20:28:13 +01:00
// it is user
2022-03-20 02:20:54 +01:00
b . users [ id ] = contact
2019-02-21 20:28:13 +01:00
}
}
// get user avatar asynchronously
2022-03-20 02:20:54 +01:00
go func ( ) {
b . Log . Debug ( "Getting user avatars.." )
2022-02-04 00:33:50 +01:00
2022-03-20 02:20:54 +01:00
for jid := range b . users {
info , err := b . GetProfilePicThumb ( jid )
if err != nil {
b . Log . Warnf ( "Could not get profile photo of %s: %v" , jid , err )
} else {
b . Lock ( )
2022-03-13 01:01:42 +01:00
b . userAvatars [ jid ] = info . URL
2022-03-20 02:20:54 +01:00
b . Unlock ( )
2022-03-13 01:01:42 +01:00
}
2019-02-21 20:28:13 +01:00
}
2022-03-20 02:20:54 +01:00
b . Log . Debug ( "Finished getting avatars.." )
} ( )
return nil
}
// Login to WhatsApp creating a new session. This will require to scan a QR code on your mobile device
func ( b * Bwhatsapp ) Login ( ) error {
b . Log . Debugln ( "Logging in.." )
invert := b . GetBool ( qrOnWhiteTerminal ) // false is the default
qrChan := qrFromTerminal ( invert )
session , err := b . conn . Login ( qrChan )
if err != nil {
b . Log . Warnln ( "Failed to log in:" , err )
return err
2022-02-04 00:33:50 +01:00
}
2020-11-27 00:34:44 +01:00
2022-03-20 02:20:54 +01:00
b . session = & session
b . Log . Infof ( "Logged into session: %#v" , session )
b . Log . Infof ( "Connection: %#v" , b . conn )
err = b . writeSession ( session )
if err != nil {
fmt . Fprintf ( os . Stderr , "error saving session: %v\n" , err )
}
2019-02-21 20:28:13 +01:00
return nil
}
// Disconnect is called while reconnecting to the bridge
// Required implementation of the Bridger interface
func ( b * Bwhatsapp ) Disconnect ( ) error {
2022-03-20 02:20:54 +01:00
// We could Logout, but that would close the session completely and would require a new QR code scan
// https://github.com/Rhymen/go-whatsapp/blob/c31092027237441cffba1b9cb148eadf7c83c3d2/session.go#L377-L381
2019-02-21 20:28:13 +01:00
return nil
}
// JoinChannel Join a WhatsApp group specified in gateway config as channel='number-id@g.us' or channel='Channel name'
// Required implementation of the Bridger interface
// https://github.com/42wim/matterbridge/blob/2cfd880cdb0df29771bf8f31df8d990ab897889d/bridge/bridge.go#L11-L16
func ( b * Bwhatsapp ) JoinChannel ( channel config . ChannelInfo ) error {
byJid := isGroupJid ( channel . Name )
2022-03-20 02:20:54 +01:00
// see https://github.com/Rhymen/go-whatsapp/issues/137#issuecomment-480316013
for len ( b . conn . Store . Contacts ) == 0 {
b . conn . Contacts ( ) // nolint:errcheck
<- time . After ( 1 * time . Second )
2022-02-03 21:48:55 +01:00
}
2019-02-21 20:28:13 +01:00
// verify if we are member of the given group
if byJid {
2022-03-20 02:20:54 +01:00
// channel.Name specifies static group jID, not the name
if _ , exists := b . conn . Store . Contacts [ channel . Name ] ; ! exists {
return fmt . Errorf ( "account doesn't belong to group with jid %s" , channel . Name )
2022-01-31 00:13:37 +01:00
}
2022-03-20 02:20:54 +01:00
return nil
2020-11-27 00:34:44 +01:00
}
2019-02-21 20:28:13 +01:00
2022-03-20 02:20:54 +01:00
// channel.Name specifies group name that might change, warn about it
var jids [ ] string
for id , contact := range b . conn . Store . Contacts {
if isGroupJid ( id ) && contact . Name == channel . Name {
jids = append ( jids , id )
2019-02-21 20:28:13 +01:00
}
}
2022-03-20 02:20:54 +01:00
switch len ( jids ) {
2020-11-27 00:34:44 +01:00
case 0 :
// didn't match any group - print out possibilites
2022-03-20 02:20:54 +01:00
for id , contact := range b . conn . Store . Contacts {
if isGroupJid ( id ) {
b . Log . Infof ( "%s %s" , contact . Jid , contact . Name )
}
2020-11-27 00:34:44 +01:00
}
2022-03-20 02:20:54 +01:00
2020-11-27 00:34:44 +01:00
return fmt . Errorf ( "please specify group's JID from the list above instead of the name '%s'" , channel . Name )
case 1 :
2022-03-20 02:20:54 +01:00
return fmt . Errorf ( "group name might change. Please configure gateway with channel=\"%v\" instead of channel=\"%v\"" , jids [ 0 ] , channel . Name )
2020-11-27 00:34:44 +01:00
default :
2022-03-20 02:20:54 +01:00
return fmt . Errorf ( "there is more than one group with name '%s'. Please specify one of JIDs as channel name: %v" , channel . Name , jids )
2020-11-27 00:34:44 +01:00
}
2019-02-21 20:28:13 +01:00
}
2020-01-09 18:14:01 +01:00
// Post a document message from the bridge to WhatsApp
func ( b * Bwhatsapp ) PostDocumentMessage ( msg config . Message , filetype string ) ( string , error ) {
fi := msg . Extra [ "file" ] [ 0 ] . ( config . FileInfo )
// Post document message
2022-03-20 02:20:54 +01:00
message := whatsapp . DocumentMessage {
Info : whatsapp . MessageInfo {
RemoteJid : msg . Channel ,
} ,
Title : fi . Name ,
FileName : fi . Name ,
Type : filetype ,
Content : bytes . NewReader ( * fi . Data ) ,
2020-01-09 18:14:01 +01:00
}
b . Log . Debugf ( "=> Sending %#v" , msg )
2022-03-20 02:20:54 +01:00
// create message ID
// TODO follow and act if https://github.com/Rhymen/go-whatsapp/issues/101 implemented
idBytes := make ( [ ] byte , 10 )
if _ , err := rand . Read ( idBytes ) ; err != nil {
b . Log . Warn ( err . Error ( ) )
}
message . Info . Id = strings . ToUpper ( hex . EncodeToString ( idBytes ) )
_ , err := b . conn . Send ( message )
2020-01-09 18:14:01 +01:00
2022-03-20 02:20:54 +01:00
return message . Info . Id , err
2020-01-09 18:14:01 +01:00
}
// Post an image message from the bridge to WhatsApp
// Handle, for sure image/jpeg, image/png and image/gif MIME types
func ( b * Bwhatsapp ) PostImageMessage ( msg config . Message , filetype string ) ( string , error ) {
fi := msg . Extra [ "file" ] [ 0 ] . ( config . FileInfo )
2022-03-20 02:20:54 +01:00
// Post image message
message := whatsapp . ImageMessage {
Info : whatsapp . MessageInfo {
RemoteJid : msg . Channel ,
} ,
Type : filetype ,
Caption : msg . Username + fi . Comment ,
Content : bytes . NewReader ( * fi . Data ) ,
2020-01-09 18:14:01 +01:00
}
2022-03-20 02:20:54 +01:00
b . Log . Debugf ( "=> Sending %#v" , msg )
2020-01-09 18:14:01 +01:00
2022-03-20 02:20:54 +01:00
// create message ID
// TODO follow and act if https://github.com/Rhymen/go-whatsapp/issues/101 implemented
idBytes := make ( [ ] byte , 10 )
if _ , err := rand . Read ( idBytes ) ; err != nil {
b . Log . Warn ( err . Error ( ) )
2020-01-09 18:14:01 +01:00
}
2022-03-20 02:20:54 +01:00
message . Info . Id = strings . ToUpper ( hex . EncodeToString ( idBytes ) )
_ , err := b . conn . Send ( message )
2020-01-09 18:14:01 +01:00
2022-03-20 02:20:54 +01:00
return message . Info . Id , err
2020-01-09 18:14:01 +01:00
}
2019-02-21 20:28:13 +01:00
// Send a message from the bridge to WhatsApp
2022-03-20 02:20:54 +01:00
// Required implementation of the Bridger interface
// https://github.com/42wim/matterbridge/blob/2cfd880cdb0df29771bf8f31df8d990ab897889d/bridge/bridge.go#L11-L16
2019-02-21 20:28:13 +01:00
func ( b * Bwhatsapp ) Send ( msg config . Message ) ( string , error ) {
b . Log . Debugf ( "=> Receiving %#v" , msg )
// Delete message
if msg . Event == config . EventMsgDelete {
if msg . ID == "" {
// No message ID in case action is executed on a message sent before the bridge was started
// and then the bridge cache doesn't have this message ID mapped
return "" , nil
}
2020-11-28 22:18:36 +01:00
2022-03-20 02:20:54 +01:00
_ , err := b . conn . RevokeMessage ( msg . Channel , msg . ID , true )
2020-11-28 22:18:36 +01:00
return "" , err
2019-02-21 20:28:13 +01:00
}
// Edit message
if msg . ID != "" {
b . Log . Debugf ( "updating message with id %s" , msg . ID )
2022-02-06 23:56:54 +01:00
if b . GetString ( "editsuffix" ) != "" {
msg . Text += b . GetString ( "EditSuffix" )
} else {
msg . Text += " (edited)"
}
2019-02-21 20:28:13 +01:00
}
2020-01-09 18:14:01 +01:00
// Handle Upload a file
if msg . Extra [ "file" ] != nil {
fi := msg . Extra [ "file" ] [ 0 ] . ( config . FileInfo )
filetype := mime . TypeByExtension ( filepath . Ext ( fi . Name ) )
b . Log . Debugf ( "Extra file is %#v" , filetype )
// TODO: add different types
// TODO: add webp conversion
switch filetype {
case "image/jpeg" , "image/png" , "image/gif" :
return b . PostImageMessage ( msg , filetype )
default :
return b . PostDocumentMessage ( msg , filetype )
}
}
2019-02-21 20:28:13 +01:00
2022-03-20 02:20:54 +01:00
// Post text message
message := whatsapp . TextMessage {
Info : whatsapp . MessageInfo {
RemoteJid : msg . Channel , // which equals to group id
} ,
Text : msg . Username + msg . Text ,
}
2022-03-13 18:05:46 +01:00
2022-03-20 02:20:54 +01:00
b . Log . Debugf ( "=> Sending %#v" , msg )
2019-02-21 20:28:13 +01:00
2022-03-20 02:20:54 +01:00
return b . conn . Send ( message )
2022-01-31 00:13:37 +01:00
}
2022-03-20 02:20:54 +01:00
// TODO do we want that? to allow login with QR code from a bridged channel? https://github.com/tulir/mautrix-whatsapp/blob/513eb18e2d59bada0dd515ee1abaaf38a3bfe3d5/commands.go#L76
//func (b *Bwhatsapp) Command(cmd string) string {
// return ""
//}