Created
January 17, 2023 21:15
-
-
Save IamNator/a1b3e8852cf90b739757a4bf079e97be to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"bytes" | |
"encoding/json" | |
"fmt" | |
"net/http" | |
) | |
type checkContacts struct { | |
Blocking string `json:"blocking"` | |
Contacts []string `json:"contacts"` | |
ForceCheck bool `json:"force_check"` | |
} | |
type contactStatus struct { | |
WAID string `json:"wa_id"` | |
Input string `json:"input"` | |
Status string `json:"status"` | |
} | |
type checkContactsResponse struct { | |
Contacts []contactStatus `json:"contacts"` | |
} | |
func checkNumberOnWhatsApp(apiURL string, apiToken string, phoneNumber string, blocking string) (bool, error) { | |
data := &checkContacts{ | |
Blocking: blocking, | |
Contacts: []string{phoneNumber}, | |
ForceCheck: false, | |
} | |
jsonValue, _ := json.Marshal(data) | |
request, err := http.NewRequest("POST", apiURL, bytes.NewBuffer(jsonValue)) | |
if err != nil { | |
return false, err | |
} | |
request.Header.Set("Authorization", "Bearer "+apiToken) | |
request.Header.Set("Content-Type", "application/json") | |
client := &http.Client{} | |
response, err := client.Do(request) | |
if err != nil { | |
return false, err | |
} | |
defer response.Body.Close() | |
var checkContactsResp checkContactsResponse | |
json.NewDecoder(response.Body).Decode(&checkContactsResp) | |
if len(checkContactsResp.Contacts)>0 { | |
contact := checkContactsResp.Contacts[0] | |
if contact.Status == "valid" { | |
return true, nil | |
} | |
} | |
return false, nil | |
} | |
func main() { | |
apiURL := "https://api.whatsapp.com/v1/contacts" | |
apiToken := "YOUR_API_TOKEN" | |
phoneNumber := "1234567890" | |
onWhatsApp, err := checkNumberOnWhatsApp(apiURL, apiToken, phoneNumber, "no_wait") | |
if err != nil { | |
fmt.Println(err) | |
return | |
} | |
if onWhatsApp { | |
fmt.Println(phoneNumber, "is on WhatsApp") | |
} else { | |
fmt.Println(phoneNumber, "is not on WhatsApp") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment