From d8ba5ac3f9faa6a6aaa4aed9aa463b2fcbee8fb9 Mon Sep 17 00:00:00 2001 From: Dimas Restu H Date: Mon, 18 Mar 2024 20:30:53 +0700 Subject: [PATCH] add send poll message in whatsapp package --- pkg/whatsapp/whatsapp.go | 48 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/pkg/whatsapp/whatsapp.go b/pkg/whatsapp/whatsapp.go index f50f1c4..67b1820 100644 --- a/pkg/whatsapp/whatsapp.go +++ b/pkg/whatsapp/whatsapp.go @@ -1070,6 +1070,54 @@ func WhatsAppSendSticker(ctx context.Context, jid string, rjid string, stickerBy return "", errors.New("WhatsApp Client is not Valid") } +func WhatsAppSendPoll(ctx context.Context, jid string, rjid string, question string, options []string, isMultipleAnswer bool) (string, error) { + if WhatsAppClient[jid] != nil { + var err error + + // Make Sure WhatsApp Client is OK + err = WhatsAppIsClientOK(jid) + if err != nil { + return "", err + } + + // Make Sure WhatsApp ID is Registered + remoteJID, err := WhatsAppCheckJID(jid, rjid) + if err != nil { + return "", err + } + + // Set Chat Presence + WhatsAppPresence(jid, true) + WhatsAppComposeStatus(jid, remoteJID, true, false) + defer func() { + WhatsAppComposeStatus(jid, remoteJID, false, false) + WhatsAppPresence(jid, false) + }() + + // Check if Poll Allow Multiple Answer + pollAnswerMax := 1 + if isMultipleAnswer { + pollAnswerMax = len(options) + } + + // Compose WhatsApp Proto + msgExtra := whatsmeow.SendRequestExtra{ + ID: WhatsAppClient[jid].GenerateMessageID(), + } + + // Send WhatsApp Message Proto + _, err = WhatsAppClient[jid].SendMessage(ctx, remoteJID, WhatsAppClient[jid].BuildPollCreation(question, options, pollAnswerMax)) + if err != nil { + return "", err + } + + return msgExtra.ID, nil + } + + // Return Error WhatsApp Client is not Valid + return "", errors.New("WhatsApp Client is not Valid") +} + func WhatsAppMessageEdit(ctx context.Context, jid string, rjid string, msgid string, message string) (string, error) { if WhatsAppClient[jid] != nil { var err error