2019-02-21 19:28:13 +00:00
|
|
|
package bwhatsapp
|
|
|
|
|
|
|
|
import (
|
2022-01-30 23:13:37 +00:00
|
|
|
"context"
|
2019-02-21 19:28:13 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2020-01-09 17:14:01 +00:00
|
|
|
"mime"
|
2019-02-21 19:28:13 +00:00
|
|
|
"os"
|
2020-01-09 17:14:01 +00:00
|
|
|
"path/filepath"
|
2019-02-21 19:28:13 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/42wim/matterbridge/bridge"
|
|
|
|
"github.com/42wim/matterbridge/bridge/config"
|
2022-01-30 23:13:37 +00:00
|
|
|
"github.com/mdp/qrterminal"
|
|
|
|
|
|
|
|
"go.mau.fi/whatsmeow"
|
|
|
|
"go.mau.fi/whatsmeow/binary/proto"
|
|
|
|
"go.mau.fi/whatsmeow/types"
|
|
|
|
waLog "go.mau.fi/whatsmeow/util/log"
|
|
|
|
|
|
|
|
goproto "google.golang.org/protobuf/proto"
|
|
|
|
|
|
|
|
_ "modernc.org/sqlite" // needed for sqlite
|
2019-02-21 19:28:13 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// Account config parameters
|
2022-01-30 23:13:37 +00:00
|
|
|
cfgNumber = "Number"
|
2019-02-21 19:28:13 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Bwhatsapp Bridge structure keeping all the information needed for relying
|
|
|
|
type Bwhatsapp struct {
|
|
|
|
*bridge.Config
|
|
|
|
|
2022-01-30 23:13:37 +00:00
|
|
|
startedAt time.Time
|
|
|
|
wc *whatsmeow.Client
|
|
|
|
contacts map[types.JID]types.ContactInfo
|
|
|
|
users map[string]types.ContactInfo
|
2019-02-21 19:28:13 +00: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-26 23:34:44 +00:00
|
|
|
|
2019-02-21 19:28:13 +00:00
|
|
|
if number == "" {
|
|
|
|
cfg.Log.Fatalf("Missing configuration for WhatsApp bridge: Number")
|
|
|
|
}
|
|
|
|
|
|
|
|
b := &Bwhatsapp{
|
|
|
|
Config: cfg,
|
|
|
|
|
2022-01-30 23:13:37 +00:00
|
|
|
users: make(map[string]types.ContactInfo),
|
2019-02-21 19:28:13 +00:00
|
|
|
userAvatars: make(map[string]string),
|
|
|
|
}
|
2020-11-26 23:34:44 +00:00
|
|
|
|
2019-02-21 19:28:13 +00:00
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
|
|
|
// Connect to WhatsApp. Required implementation of the Bridger interface
|
|
|
|
func (b *Bwhatsapp) Connect() error {
|
2022-01-30 23:13:37 +00:00
|
|
|
device, err := b.getDevice()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-02-21 19:28:13 +00:00
|
|
|
number := b.GetString(cfgNumber)
|
|
|
|
if number == "" {
|
2020-11-26 23:34:44 +00:00
|
|
|
return errors.New("whatsapp's telephone number need to be configured")
|
2019-02-21 19:28:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
b.Log.Debugln("Connecting to WhatsApp..")
|
|
|
|
|
2022-01-30 23:13:37 +00:00
|
|
|
b.wc = whatsmeow.NewClient(device, waLog.Stdout("Client", "INFO", true))
|
|
|
|
b.wc.AddEventHandler(b.eventHandler)
|
2019-02-21 19:28:13 +00:00
|
|
|
|
2022-01-30 23:13:37 +00:00
|
|
|
// No ID stored, new login
|
|
|
|
qrChan, err := b.wc.GetQRChannel(context.Background())
|
|
|
|
// This error means that we're already logged in, so ignore it.
|
|
|
|
if err != nil && !errors.Is(err, whatsmeow.ErrQRStoreContainsID) {
|
|
|
|
return errors.New("failed to to get QR channel:" + err.Error())
|
|
|
|
}
|
2019-02-21 19:28:13 +00:00
|
|
|
|
2022-01-30 23:13:37 +00:00
|
|
|
err = b.wc.Connect()
|
2020-11-26 23:34:44 +00:00
|
|
|
if err != nil {
|
2022-01-30 23:13:37 +00:00
|
|
|
return errors.New("failed to connect to WhatsApp: " + err.Error())
|
2019-02-21 19:28:13 +00:00
|
|
|
}
|
|
|
|
|
2022-01-30 23:13:37 +00:00
|
|
|
for evt := range qrChan {
|
|
|
|
if evt.Event == "code" {
|
|
|
|
// Render the QR code here
|
|
|
|
qrterminal.GenerateHalfBlock(evt.Code, qrterminal.L, os.Stdout)
|
|
|
|
} else {
|
|
|
|
b.Log.Infof("QR channel result: %s", evt.Event)
|
2019-02-21 19:28:13 +00:00
|
|
|
}
|
|
|
|
}
|
2020-11-26 23:34:44 +00:00
|
|
|
|
2022-01-30 23:13:37 +00:00
|
|
|
b.Log.Infoln("WhatsApp connection successful")
|
2019-02-21 19:28:13 +00:00
|
|
|
|
2022-01-30 23:13:37 +00:00
|
|
|
b.contacts, err = b.wc.Store.Contacts.GetAllContacts()
|
2019-02-21 19:28:13 +00:00
|
|
|
if err != nil {
|
2022-01-30 23:13:37 +00:00
|
|
|
return errors.New("failed to get contacts: " + err.Error())
|
2019-02-21 19:28:13 +00:00
|
|
|
}
|
|
|
|
|
2022-01-30 23:13:37 +00:00
|
|
|
b.startedAt = time.Now()
|
2020-11-22 20:42:54 +00:00
|
|
|
|
2019-02-21 19:28:13 +00:00
|
|
|
// map all the users
|
2022-01-30 23:13:37 +00:00
|
|
|
for id, contact := range b.contacts {
|
|
|
|
if !isGroupJid(id.String()) && id.String() != "status@broadcast" {
|
2019-02-21 19:28:13 +00:00
|
|
|
// it is user
|
2022-01-30 23:13:37 +00:00
|
|
|
b.users[id.String()] = contact
|
2019-02-21 19:28:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// get user avatar asynchronously
|
|
|
|
go func() {
|
|
|
|
b.Log.Debug("Getting user avatars..")
|
|
|
|
|
|
|
|
for jid := range b.users {
|
2019-05-31 22:53:49 +00:00
|
|
|
info, err := b.GetProfilePicThumb(jid)
|
2019-02-21 19:28:13 +00:00
|
|
|
if err != nil {
|
|
|
|
b.Log.Warnf("Could not get profile photo of %s: %v", jid, err)
|
|
|
|
} else {
|
2020-11-26 23:34:44 +00:00
|
|
|
b.Lock()
|
2019-02-21 19:28:13 +00:00
|
|
|
b.userAvatars[jid] = info.URL
|
2020-11-26 23:34:44 +00:00
|
|
|
b.Unlock()
|
2019-02-21 19:28:13 +00:00
|
|
|
}
|
|
|
|
}
|
2020-11-26 23:34:44 +00:00
|
|
|
|
2019-02-21 19:28:13 +00:00
|
|
|
b.Log.Debug("Finished getting avatars..")
|
|
|
|
}()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Disconnect is called while reconnecting to the bridge
|
|
|
|
// Required implementation of the Bridger interface
|
|
|
|
func (b *Bwhatsapp) Disconnect() error {
|
2022-01-30 23:13:37 +00:00
|
|
|
b.wc.Disconnect()
|
|
|
|
|
2019-02-21 19:28:13 +00: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)
|
|
|
|
|
|
|
|
// verify if we are member of the given group
|
|
|
|
if byJid {
|
2022-01-30 23:13:37 +00:00
|
|
|
gJID, err := types.ParseJID(channel.Name)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-02-21 19:28:13 +00:00
|
|
|
// channel.Name specifies static group jID, not the name
|
2022-01-30 23:13:37 +00:00
|
|
|
info, err := b.wc.Store.Contacts.GetContact(gJID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !info.Found {
|
2019-02-21 19:28:13 +00:00
|
|
|
return fmt.Errorf("account doesn't belong to group with jid %s", channel.Name)
|
|
|
|
}
|
|
|
|
|
2020-11-26 23:34:44 +00:00
|
|
|
return nil
|
|
|
|
}
|
2019-02-21 19:28:13 +00:00
|
|
|
|
2020-11-26 23:34:44 +00:00
|
|
|
// channel.Name specifies group name that might change, warn about it
|
|
|
|
var jids []string
|
2022-01-30 23:13:37 +00:00
|
|
|
allContacts, err := b.wc.Store.Contacts.GetAllContacts()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for id := range allContacts {
|
|
|
|
if isGroupJid(id.String()) && id.String() == channel.Name {
|
|
|
|
jids = append(jids, id.String())
|
2019-02-21 19:28:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-26 23:34:44 +00:00
|
|
|
switch len(jids) {
|
|
|
|
case 0:
|
|
|
|
// didn't match any group - print out possibilites
|
2022-01-30 23:13:37 +00:00
|
|
|
for id, contact := range allContacts {
|
|
|
|
if isGroupJid(id.String()) {
|
|
|
|
b.Log.Infof("%s %s", id, contact.FullName)
|
2020-11-26 23:34:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Errorf("please specify group's JID from the list above instead of the name '%s'", channel.Name)
|
|
|
|
case 1:
|
|
|
|
return fmt.Errorf("group name might change. Please configure gateway with channel=\"%v\" instead of channel=\"%v\"", jids[0], channel.Name)
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("there is more than one group with name '%s'. Please specify one of JIDs as channel name: %v", channel.Name, jids)
|
|
|
|
}
|
2019-02-21 19:28:13 +00:00
|
|
|
}
|
|
|
|
|
2020-01-09 17:14:01 +00:00
|
|
|
// Post a document message from the bridge to WhatsApp
|
|
|
|
func (b *Bwhatsapp) PostDocumentMessage(msg config.Message, filetype string) (string, error) {
|
2022-01-30 23:13:37 +00:00
|
|
|
groupJID, _ := types.ParseJID(msg.Channel)
|
|
|
|
|
2020-01-09 17:14:01 +00:00
|
|
|
fi := msg.Extra["file"][0].(config.FileInfo)
|
|
|
|
|
2022-01-30 23:13:37 +00:00
|
|
|
resp, err := b.wc.Upload(context.Background(), *fi.Data, whatsmeow.MediaDocument)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2020-01-09 17:14:01 +00:00
|
|
|
// Post document message
|
2022-01-30 23:13:37 +00:00
|
|
|
var message proto.Message
|
|
|
|
|
|
|
|
message.DocumentMessage = &proto.DocumentMessage{
|
|
|
|
Title: &fi.Name,
|
|
|
|
FileName: &fi.Name,
|
|
|
|
Mimetype: &filetype,
|
|
|
|
MediaKey: resp.MediaKey,
|
|
|
|
FileEncSha256: resp.FileEncSHA256,
|
|
|
|
FileSha256: resp.FileSHA256,
|
|
|
|
FileLength: goproto.Uint64(resp.FileLength),
|
|
|
|
Url: &resp.URL,
|
2020-01-09 17:14:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
b.Log.Debugf("=> Sending %#v", msg)
|
|
|
|
|
2022-01-30 23:13:37 +00:00
|
|
|
ID := whatsmeow.GenerateMessageID()
|
|
|
|
_, err = b.wc.SendMessage(groupJID, ID, &message)
|
2020-01-09 17:14:01 +00:00
|
|
|
|
2022-01-30 23:13:37 +00:00
|
|
|
return ID, err
|
2020-01-09 17:14:01 +00: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) {
|
2022-01-30 23:13:37 +00:00
|
|
|
groupJID, _ := types.ParseJID(msg.Channel)
|
|
|
|
|
2020-01-09 17:14:01 +00:00
|
|
|
fi := msg.Extra["file"][0].(config.FileInfo)
|
|
|
|
|
2022-01-30 23:13:37 +00:00
|
|
|
caption := msg.Username + fi.Comment
|
|
|
|
|
|
|
|
resp, err := b.wc.Upload(context.Background(), *fi.Data, whatsmeow.MediaImage)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
2020-01-09 17:14:01 +00:00
|
|
|
}
|
|
|
|
|
2022-01-30 23:13:37 +00:00
|
|
|
var message proto.Message
|
2020-01-09 17:14:01 +00:00
|
|
|
|
2022-01-30 23:13:37 +00:00
|
|
|
message.ImageMessage = &proto.ImageMessage{
|
|
|
|
Mimetype: &filetype,
|
|
|
|
Caption: &caption,
|
|
|
|
MediaKey: resp.MediaKey,
|
|
|
|
FileEncSha256: resp.FileEncSHA256,
|
|
|
|
FileSha256: resp.FileSHA256,
|
|
|
|
FileLength: goproto.Uint64(resp.FileLength),
|
|
|
|
Url: &resp.URL,
|
2020-01-09 17:14:01 +00:00
|
|
|
}
|
|
|
|
|
2022-01-30 23:13:37 +00:00
|
|
|
b.Log.Debugf("=> Sending %#v", msg)
|
|
|
|
|
|
|
|
ID := whatsmeow.GenerateMessageID()
|
|
|
|
_, err = b.wc.SendMessage(groupJID, ID, &message)
|
2020-01-09 17:14:01 +00:00
|
|
|
|
2022-01-30 23:13:37 +00:00
|
|
|
return ID, err
|
2020-01-09 17:14:01 +00:00
|
|
|
}
|
|
|
|
|
2019-02-21 19:28:13 +00:00
|
|
|
// Send a message from the bridge to WhatsApp
|
|
|
|
func (b *Bwhatsapp) Send(msg config.Message) (string, error) {
|
2022-01-30 23:13:37 +00:00
|
|
|
groupJID, _ := types.ParseJID(msg.Channel)
|
|
|
|
|
2019-02-21 19:28:13 +00:00
|
|
|
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 21:18:36 +00:00
|
|
|
|
2022-01-30 23:13:37 +00:00
|
|
|
_, err := b.wc.RevokeMessage(groupJID, msg.ID)
|
2020-11-28 21:18:36 +00:00
|
|
|
|
|
|
|
return "", err
|
2019-02-21 19:28:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Edit message
|
|
|
|
if msg.ID != "" {
|
|
|
|
b.Log.Debugf("updating message with id %s", msg.ID)
|
|
|
|
|
2022-02-06 22:56:54 +00:00
|
|
|
if b.GetString("editsuffix") != "" {
|
|
|
|
msg.Text += b.GetString("EditSuffix")
|
|
|
|
} else {
|
|
|
|
msg.Text += " (edited)"
|
|
|
|
}
|
2019-02-21 19:28:13 +00:00
|
|
|
}
|
|
|
|
|
2020-01-09 17:14:01 +00: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 19:28:13 +00:00
|
|
|
|
2022-01-30 23:13:37 +00:00
|
|
|
var message proto.Message
|
|
|
|
|
|
|
|
text := msg.Username + msg.Text
|
|
|
|
|
|
|
|
message.ExtendedTextMessage = &proto.ExtendedTextMessage{
|
|
|
|
Text: &text,
|
2019-02-21 19:28:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
b.Log.Debugf("=> Sending %#v", msg)
|
|
|
|
|
2022-01-30 23:13:37 +00:00
|
|
|
ID := whatsmeow.GenerateMessageID()
|
|
|
|
_, err := b.wc.SendMessage(groupJID, ID, &message)
|
2019-02-21 19:28:13 +00:00
|
|
|
|
2022-01-30 23:13:37 +00:00
|
|
|
return ID, err
|
|
|
|
}
|