Skip to content

Commit

Permalink
Merge pull request #267 from SchulteMK/cleanup
Browse files Browse the repository at this point in the history
General cleanup
  • Loading branch information
SchulteMK authored Dec 31, 2019
2 parents 5282d89 + eda8ed7 commit 1a11f6d
Showing 1 changed file with 11 additions and 26 deletions.
37 changes: 11 additions & 26 deletions message.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,68 +23,53 @@ const (
MediaDocument MediaType = "WhatsApp Document Keys"
)

var msgInfo MessageInfo

func (wac *Conn) Send(msg interface{}) (string, error) {
var err error
var ch <-chan string
var msgProto *proto.WebMessageInfo

switch m := msg.(type) {
case *proto.WebMessageInfo:
ch, err = wac.sendProto(m)
msgProto = m
case TextMessage:
msgProto = getTextProto(m)
msgInfo = getMessageInfo(msgProto)
ch, err = wac.sendProto(msgProto)
case ImageMessage:
var err error
m.url, m.mediaKey, m.fileEncSha256, m.fileSha256, m.fileLength, err = wac.Upload(m.Content, MediaImage)
if err != nil {
return "ERROR", fmt.Errorf("image upload failed: %v", err)
}
msgProto = getImageProto(m)
msgInfo = getMessageInfo(msgProto)
ch, err = wac.sendProto(msgProto)
case VideoMessage:
var err error
m.url, m.mediaKey, m.fileEncSha256, m.fileSha256, m.fileLength, err = wac.Upload(m.Content, MediaVideo)
if err != nil {
return "ERROR", fmt.Errorf("video upload failed: %v", err)
}
msgProto = getVideoProto(m)
msgInfo = getMessageInfo(msgProto)
ch, err = wac.sendProto(msgProto)
case DocumentMessage:
var err error
m.url, m.mediaKey, m.fileEncSha256, m.fileSha256, m.fileLength, err = wac.Upload(m.Content, MediaDocument)
if err != nil {
return "ERROR", fmt.Errorf("document upload failed: %v", err)
}
msgProto = getDocumentProto(m)
msgInfo = getMessageInfo(msgProto)
ch, err = wac.sendProto(msgProto)
case AudioMessage:
var err error
m.url, m.mediaKey, m.fileEncSha256, m.fileSha256, m.fileLength, err = wac.Upload(m.Content, MediaAudio)
if err != nil {
return "ERROR", fmt.Errorf("audio upload failed: %v", err)
}
msgProto = getAudioProto(m)
msgInfo = getMessageInfo(msgProto)
ch, err = wac.sendProto(msgProto)
case LocationMessage:
msgProto = GetLocationProto(m)
msgInfo = getMessageInfo(msgProto)
ch, err = wac.sendProto(msgProto)
case LiveLocationMessage:
msgProto = GetLiveLocationProto(m)
msgInfo = getMessageInfo(msgProto)
ch, err = wac.sendProto(msgProto)
case ContactMessage:
msgProto = getContactMessageProto(m)
msgInfo = getMessageInfo(msgProto)
ch, err = wac.sendProto(msgProto)
default:
return "ERROR", fmt.Errorf("cannot match type %T, use message types declared in the package", msg)
}

ch, err := wac.sendProto(msgProto)
if err != nil {
return "ERROR", fmt.Errorf("could not send proto: %v", err)
}
Expand All @@ -99,7 +84,7 @@ func (wac *Conn) Send(msg interface{}) (string, error) {
return "ERROR", fmt.Errorf("message sending responded with %d", resp["status"])
}
if int(resp["status"].(float64)) == 200 {
return msgInfo.Id, nil
return getMessageInfo(msgProto).Id, nil
}
case <-time.After(wac.msgTimeout):
return "ERROR", fmt.Errorf("sending message timed out")
Expand Down Expand Up @@ -654,7 +639,7 @@ type StickerMessage struct {
func getStickerMessage(msg *proto.WebMessageInfo) StickerMessage {
sticker := msg.GetMessage().GetStickerMessage()

StickerMessage := StickerMessage{
stickerMessage := StickerMessage{
Info: getMessageInfo(msg),
url: sticker.GetUrl(),
mediaKey: sticker.GetMediaKey(),
Expand All @@ -665,7 +650,7 @@ func getStickerMessage(msg *proto.WebMessageInfo) StickerMessage {
ContextInfo: getMessageContext(sticker.GetContextInfo()),
}

return StickerMessage
return stickerMessage
}

/*
Expand All @@ -691,7 +676,7 @@ type ContactMessage struct {
func getContactMessage(msg *proto.WebMessageInfo) ContactMessage {
contact := msg.GetMessage().GetContactMessage()

ContactMessage := ContactMessage{
contactMessage := ContactMessage{
Info: getMessageInfo(msg),

DisplayName: contact.GetDisplayName(),
Expand All @@ -700,7 +685,7 @@ func getContactMessage(msg *proto.WebMessageInfo) ContactMessage {
ContextInfo: getMessageContext(contact.GetContextInfo()),
}

return ContactMessage
return contactMessage
}

func getContactMessageProto(msg ContactMessage) *proto.WebMessageInfo {
Expand Down

0 comments on commit 1a11f6d

Please sign in to comment.