Clean&combed code
This commit is contained in:
@@ -1,13 +1,13 @@
|
|||||||
package bwhatsapp
|
package bwhatsapp
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
|
"mime"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
"mime"
|
|
||||||
"fmt"
|
|
||||||
|
|
||||||
"github.com/42wim/matterbridge/bridge/config"
|
"github.com/42wim/matterbridge/bridge/config"
|
||||||
"github.com/42wim/matterbridge/bridge/helper"
|
"github.com/42wim/matterbridge/bridge/helper"
|
||||||
|
|
||||||
"github.com/matterbridge/go-whatsapp"
|
"github.com/matterbridge/go-whatsapp"
|
||||||
|
|
||||||
|
|||||||
@@ -1,16 +1,16 @@
|
|||||||
package bwhatsapp
|
package bwhatsapp
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"crypto/rand"
|
"crypto/rand"
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"mime"
|
||||||
"os"
|
"os"
|
||||||
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
"bytes"
|
|
||||||
"mime"
|
|
||||||
"path/filepath"
|
|
||||||
|
|
||||||
"github.com/42wim/matterbridge/bridge"
|
"github.com/42wim/matterbridge/bridge"
|
||||||
"github.com/42wim/matterbridge/bridge/config"
|
"github.com/42wim/matterbridge/bridge/config"
|
||||||
@@ -277,8 +277,8 @@ func (b *Bwhatsapp) Send(msg config.Message) (string, error) {
|
|||||||
|
|
||||||
b.Log.Debugf("Extra file is %#v", filetype)
|
b.Log.Debugf("Extra file is %#v", filetype)
|
||||||
|
|
||||||
// TODO: add different types
|
// TODO: add different types
|
||||||
// TODO: add webp conversion
|
// TODO: add webp conversion
|
||||||
if filetype == "image/jpeg" || filetype == "image/png" || filetype == "image/gif" {
|
if filetype == "image/jpeg" || filetype == "image/png" || filetype == "image/gif" {
|
||||||
// Post image message
|
// Post image message
|
||||||
message := whatsapp.ImageMessage{
|
message := whatsapp.ImageMessage{
|
||||||
@@ -302,40 +302,18 @@ func (b *Bwhatsapp) Send(msg config.Message) (string, error) {
|
|||||||
message.Info.Id = strings.ToUpper(hex.EncodeToString(idbytes))
|
message.Info.Id = strings.ToUpper(hex.EncodeToString(idbytes))
|
||||||
err := b.conn.Send(message)
|
err := b.conn.Send(message)
|
||||||
|
|
||||||
return message.Info.Id, err
|
|
||||||
} else {
|
|
||||||
// Post document message
|
|
||||||
message := whatsapp.DocumentMessage{
|
|
||||||
Info: whatsapp.MessageInfo{
|
|
||||||
RemoteJid: msg.Channel,
|
|
||||||
},
|
|
||||||
Title: fi.Name,
|
|
||||||
FileName: fi.Name,
|
|
||||||
Type: filetype,
|
|
||||||
Content: bytes.NewReader(*fi.Data),
|
|
||||||
}
|
|
||||||
|
|
||||||
b.Log.Debugf("=> Sending %#v", msg)
|
|
||||||
|
|
||||||
// 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)
|
|
||||||
|
|
||||||
return message.Info.Id, err
|
return message.Info.Id, err
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
// Post text message
|
// Post document message
|
||||||
message := whatsapp.TextMessage{
|
message := whatsapp.DocumentMessage{
|
||||||
Info: whatsapp.MessageInfo{
|
Info: whatsapp.MessageInfo{
|
||||||
RemoteJid: msg.Channel, // which equals to group id
|
RemoteJid: msg.Channel,
|
||||||
},
|
},
|
||||||
Text: msg.Username + msg.Text,
|
Title: fi.Name,
|
||||||
|
FileName: fi.Name,
|
||||||
|
Type: filetype,
|
||||||
|
Content: bytes.NewReader(*fi.Data),
|
||||||
}
|
}
|
||||||
|
|
||||||
b.Log.Debugf("=> Sending %#v", msg)
|
b.Log.Debugf("=> Sending %#v", msg)
|
||||||
@@ -352,6 +330,28 @@ func (b *Bwhatsapp) Send(msg config.Message) (string, error) {
|
|||||||
|
|
||||||
return message.Info.Id, err
|
return message.Info.Id, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Post text message
|
||||||
|
message := whatsapp.TextMessage{
|
||||||
|
Info: whatsapp.MessageInfo{
|
||||||
|
RemoteJid: msg.Channel, // which equals to group id
|
||||||
|
},
|
||||||
|
Text: msg.Username + msg.Text,
|
||||||
|
}
|
||||||
|
|
||||||
|
b.Log.Debugf("=> Sending %#v", msg)
|
||||||
|
|
||||||
|
// 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)
|
||||||
|
|
||||||
|
return message.Info.Id, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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
|
// 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
|
||||||
|
|||||||
Reference in New Issue
Block a user