In this second step, we are going to analyse how to use the API of go-whatsapp described in the previous blog post, and what considerations we should take into account in order to create a bot.
Sending a text message
The WhatsApp Connection instance that is returned when creating a Session has several messages to send text messages, images, create groups, receive messages and even query for the contact list.
For sending a text message you have to create an instance of TextMessage indicating at least the text message an a receipt.
text := whatsapp.TextMessage{
Info: whatsapp.MessageInfo{
RemoteJid: "<receiver's phone number>@s.whatsapp.net",
},
Text: "Text message sent from Golang bot",
}
sendResult, err := waconn.Send(text)
where whatsApp refers to the module go-whatsapp imported as
import (
"github.com/Rhymen/go-whatsapp"
)
whatsApp.TextMessage is an struct which fields are
type TextMessage struct {
Info MessageInfo
Text string
ContextInfo ContextInfo
}
and whatsApp.MessageInfo contains
type MessageInfo struct {
Id string
RemoteJid string
SenderJid string
FromMe bool
Timestamp uint64
PushName string
Status MessageStatus
Source *proto.WebMessageInfo
}
The first important thing to notice is that MesssageInfo only contains on RemoteJid, so it’s not possible to a single message to multiple receivers (broadcasting).
Note: Trying to concatenate several RemoteJid (separated with comma, for example) leads to a crash in the mobile app where WhatsApp runs! unbelievable.
The response to the Send command is a string and an error. Where error is nil if the operation succeeded, and string contains either the MessageId if everything is okey, or the literal string ERROR in case of failure.
Other types of messages can be sent using this module. The existent implementation of go-whatsapp, by the time this post was created, allows a user to send text, photos, videos, documents (files), audio, locations, live locations contact information (vCards), and even Stickers!.
Image, Audio and Video messages receive its content in a io.Reader property. Several fields of these structs remain unexpected as they are needed for media upload/download and validations.
The definition of each of these structures are
Image Message
type ImageMessage struct {
Info MessageInfo
Caption string
Thumbnail []byte
Type string
Content io.Reader
url string
mediaKey []byte
fileEncSha256 []byte
fileSha256 []byte
fileLength uint64
ContextInfo ContextInfo
}
Video Message
type VideoMessage struct {
Info MessageInfo
Caption string
Thumbnail []byte
Length uint32
Type string
Content io.Reader
GifPlayback bool
url string
mediaKey []byte
fileEncSha256 []byte
fileSha256 []byte
fileLength uint64
ContextInfo ContextInfo
}
Audio Message
type AudioMessage struct {
Info MessageInfo
Length uint32
Type string
Content io.Reader
Ptt bool
url string
mediaKey []byte
fileEncSha256 []byte
fileSha256 []byte
fileLength uint64
ContextInfo ContextInfo
}
Document Message
type DocumentMessage struct {
Info MessageInfo
Title string
PageCount uint32
Type string
FileName string
Thumbnail []byte
Content io.Reader
url string
mediaKey []byte
fileEncSha256 []byte
fileSha256 []byte
fileLength uint64
ContextInfo ContextInfo
}
Location Message
type LocationMessage struct {
Info MessageInfo
DegreesLatitude float64
DegreesLongitude float64
Name string
Address string
Url string
JpegThumbnail []byte
ContextInfo ContextInfo
}
Live Location Message
type LiveLocationMessage struct {
Info MessageInfo
DegreesLatitude float64
DegreesLongitude float64
AccuracyInMeters uint32
SpeedInMps float32
DegreesClockwiseFromMagneticNorth uint32
Caption string
SequenceNumber int64
JpegThumbnail []byte
ContextInfo ContextInfo
}
Stickers
type StickerMessage struct {
Info MessageInfo
Type string
Content io.Reader
url string
mediaKey []byte
fileEncSha256 []byte
fileSha256 []byte
fileLength uint64
ContextInfo ContextInfo
}
Contact Message (vCard)
type ContactMessage struct {
Info MessageInfo
DisplayName string
Vcard string
ContextInfo ContextInfo
}