Как отправить API WhatsApp с помощью Go

Введение

В этом фрагменте кода вы узнаете, как отправить WhatsApp API с помощью Go, чтобы отправить текстовое сообщение, WhatsApp Шаблоны, медиа, интерактивные кнопки, списки, продукты … и т.д., Для выполнения следующих кодов требуется учетная запись Alvochat. Создайте аккаунт, если у вас его нет.

Отправьте шаблон с помощью WhatsApp API и перейдите

package main

import (
	"fmt"
	"strings"
	"net/http"
	"io/ioutil"
    "net/url"
)

func main() {

	apiurl:= "https://api.alvochat.com/instance1199/messages/template"
    data := url.Values{}
    			 data.Set("token", "YourToken")
			 data.Set("to", "16315555555")
			 data.Set("name", "hello_world")
			 data.Set("language", "en_us")
			 data.Set("header", "")
			 data.Set("body", "")
			 data.Set("buttons", "")
			 data.Set("priority", "")

   
	payload := strings.NewReader(data.Encode())

	req, _ := http.NewRequest("POST", apiurl, payload)

	req.Header.Add("content-type", "application/x-www-form-urlencoded")

	res, _ := http.DefaultClient.Do(req)

	defer res.Body.Close()
	body, _ := ioutil.ReadAll(res.Body)

 
	fmt.Println(string(body))

}

Отправка текстового сообщения с помощью WhatsApp API и Go

package main

import (
	"fmt"
	"strings"
	"net/http"
	"io/ioutil"
    "net/url"
)

func main() {

	apiurl:= "https://api.alvochat.com/instance1199/messages/chat"
    data := url.Values{}
    			 data.Set("token", "YourToken")
			 data.Set("to", "16315555555")
			 data.Set("body", "WhatsApp API on alvochat.com works good")
			 data.Set("priority", "")
			 data.Set("preview_url", "")
			 data.Set("message_id", "")

   
	payload := strings.NewReader(data.Encode())

	req, _ := http.NewRequest("POST", apiurl, payload)

	req.Header.Add("content-type", "application/x-www-form-urlencoded")

	res, _ := http.DefaultClient.Do(req)

	defer res.Body.Close()
	body, _ := ioutil.ReadAll(res.Body)

 
	fmt.Println(string(body))

}

Отправить изображение

package main

import (
	"fmt"
	"strings"
	"net/http"
	"io/ioutil"
    "net/url"
)

func main() {

	apiurl:= "https://api.alvochat.com/instance1199/messages/image"
    data := url.Values{}
    			 data.Set("token", "YourToken")
			 data.Set("to", "16315555555")
			 data.Set("image", "https://alvochat-example.s3-accelerate.amazonaws.com/image/1.jpeg")
			 data.Set("caption", "image caption")
			 data.Set("priority", "")
			 data.Set("message_id", "")

   
	payload := strings.NewReader(data.Encode())

	req, _ := http.NewRequest("POST", apiurl, payload)

	req.Header.Add("content-type", "application/x-www-form-urlencoded")

	res, _ := http.DefaultClient.Do(req)

	defer res.Body.Close()
	body, _ := ioutil.ReadAll(res.Body)

 
	fmt.Println(string(body))

}

Отправить аудио

package main

import (
	"fmt"
	"strings"
	"net/http"
	"io/ioutil"
    "net/url"
)

func main() {

	apiurl:= "https://api.alvochat.com/instance1199/messages/audio"
    data := url.Values{}
    			 data.Set("token", "YourToken")
			 data.Set("to", "16315555555")
			 data.Set("audio", "https://alvochat-example.s3-accelerate.amazonaws.com/audio/1.mp3")
			 data.Set("priority", "")
			 data.Set("message_id", "")

   
	payload := strings.NewReader(data.Encode())

	req, _ := http.NewRequest("POST", apiurl, payload)

	req.Header.Add("content-type", "application/x-www-form-urlencoded")

	res, _ := http.DefaultClient.Do(req)

	defer res.Body.Close()
	body, _ := ioutil.ReadAll(res.Body)

 
	fmt.Println(string(body))

}

Отправить видео

package main

import (
	"fmt"
	"strings"
	"net/http"
	"io/ioutil"
    "net/url"
)

func main() {

	apiurl:= "https://api.alvochat.com/instance1199/messages/video"
    data := url.Values{}
    			 data.Set("token", "YourToken")
			 data.Set("to", "16315555555")
			 data.Set("video", "https://alvochat-example.s3-accelerate.amazonaws.com/video/1.mp4")
			 data.Set("caption", "video caption")
			 data.Set("priority", "")
			 data.Set("message_id", "")

   
	payload := strings.NewReader(data.Encode())

	req, _ := http.NewRequest("POST", apiurl, payload)

	req.Header.Add("content-type", "application/x-www-form-urlencoded")

	res, _ := http.DefaultClient.Do(req)

	defer res.Body.Close()
	body, _ := ioutil.ReadAll(res.Body)

 
	fmt.Println(string(body))

}

Отправить документ

package main

import (
	"fmt"
	"strings"
	"net/http"
	"io/ioutil"
    "net/url"
)

func main() {

	apiurl:= "https://api.alvochat.com/instance1199/messages/document"
    data := url.Values{}
    			 data.Set("token", "YourToken")
			 data.Set("to", "16315555555")
			 data.Set("document", "https://alvochat-example.s3-accelerate.amazonaws.com/document/1.pdf")
			 data.Set("filename", "")
			 data.Set("priority", "")
			 data.Set("message_id", "")

   
	payload := strings.NewReader(data.Encode())

	req, _ := http.NewRequest("POST", apiurl, payload)

	req.Header.Add("content-type", "application/x-www-form-urlencoded")

	res, _ := http.DefaultClient.Do(req)

	defer res.Body.Close()
	body, _ := ioutil.ReadAll(res.Body)

 
	fmt.Println(string(body))

}

Отправить наклейку

package main

import (
	"fmt"
	"strings"
	"net/http"
	"io/ioutil"
    "net/url"
)

func main() {

	apiurl:= "https://api.alvochat.com/instance1199/messages/sticker"
    data := url.Values{}
    			 data.Set("token", "YourToken")
			 data.Set("to", "16315555555")
			 data.Set("sticker", "https://alvochat-example.s3-accelerate.amazonaws.com/sticker/1.webp")
			 data.Set("priority", "")
			 data.Set("message_id", "")

   
	payload := strings.NewReader(data.Encode())

	req, _ := http.NewRequest("POST", apiurl, payload)

	req.Header.Add("content-type", "application/x-www-form-urlencoded")

	res, _ := http.DefaultClient.Do(req)

	defer res.Body.Close()
	body, _ := ioutil.ReadAll(res.Body)

 
	fmt.Println(string(body))

}

Отправить контакт

package main

import (
	"fmt"
	"strings"
	"net/http"
	"io/ioutil"
    "net/url"
)

func main() {

	apiurl:= "https://api.alvochat.com/instance1199/messages/contact"
    data := url.Values{}
    			 data.Set("token", "YourToken")
			 data.Set("to", "16315555555")
			 data.Set("contact", "")
			 data.Set("priority", "")
			 data.Set("message_id", "")

   
	payload := strings.NewReader(data.Encode())

	req, _ := http.NewRequest("POST", apiurl, payload)

	req.Header.Add("content-type", "application/x-www-form-urlencoded")

	res, _ := http.DefaultClient.Do(req)

	defer res.Body.Close()
	body, _ := ioutil.ReadAll(res.Body)

 
	fmt.Println(string(body))

}

Отправить местоположение

package main

import (
	"fmt"
	"strings"
	"net/http"
	"io/ioutil"
    "net/url"
)

func main() {

	apiurl:= "https://api.alvochat.com/instance1199/messages/location"
    data := url.Values{}
    			 data.Set("token", "YourToken")
			 data.Set("to", "16315555555")
			 data.Set("lat", "37.484296")
			 data.Set("lng", "-122.148703")
			 data.Set("address", "Menlo Park, California, United States")
			 data.Set("name", "Meta Headquarters")
			 data.Set("priority", "")
			 data.Set("message_id", "")

   
	payload := strings.NewReader(data.Encode())

	req, _ := http.NewRequest("POST", apiurl, payload)

	req.Header.Add("content-type", "application/x-www-form-urlencoded")

	res, _ := http.DefaultClient.Do(req)

	defer res.Body.Close()
	body, _ := ioutil.ReadAll(res.Body)

 
	fmt.Println(string(body))

}

Список отправки

package main

import (
	"fmt"
	"strings"
	"net/http"
	"io/ioutil"
    "net/url"
)

func main() {

	apiurl:= "https://api.alvochat.com/instance1199/messages/list"
    data := url.Values{}
    			 data.Set("token", "YourToken")
			 data.Set("to", "16315555555")
			 data.Set("header", "header")
			 data.Set("body", " please select one of the following options")
			 data.Set("footer", "footer")
			 data.Set("button", "options")
			 data.Set("sections", "option_1,option_2,option_3")
			 data.Set("priority", "")
			 data.Set("message_id", "")

   
	payload := strings.NewReader(data.Encode())

	req, _ := http.NewRequest("POST", apiurl, payload)

	req.Header.Add("content-type", "application/x-www-form-urlencoded")

	res, _ := http.DefaultClient.Do(req)

	defer res.Body.Close()
	body, _ := ioutil.ReadAll(res.Body)

 
	fmt.Println(string(body))

}

Кнопка отправки

package main

import (
	"fmt"
	"strings"
	"net/http"
	"io/ioutil"
    "net/url"
)

func main() {

	apiurl:= "https://api.alvochat.com/instance1199/messages/button"
    data := url.Values{}
    			 data.Set("token", "YourToken")
			 data.Set("to", "16315555555")
			 data.Set("header", "header")
			 data.Set("body", " please select one of the following options")
			 data.Set("footer", "footer")
			 data.Set("buttons", "option_1,option_2,option_3")
			 data.Set("priority", "")
			 data.Set("message_id", "")

   
	payload := strings.NewReader(data.Encode())

	req, _ := http.NewRequest("POST", apiurl, payload)

	req.Header.Add("content-type", "application/x-www-form-urlencoded")

	res, _ := http.DefaultClient.Do(req)

	defer res.Body.Close()
	body, _ := ioutil.ReadAll(res.Body)

 
	fmt.Println(string(body))

}

Отправить продукцию

package main

import (
	"fmt"
	"strings"
	"net/http"
	"io/ioutil"
    "net/url"
)

func main() {

	apiurl:= "https://api.alvochat.com/instance1199/messages/product"
    data := url.Values{}
    			 data.Set("token", "YourToken")
			 data.Set("to", "16315555555")
			 data.Set("header", "header")
			 data.Set("body", "Hi , check out our new products")
			 data.Set("footer", "footer")
			 data.Set("catalog_id", "")
			 data.Set("product", "")
			 data.Set("priority", "")
			 data.Set("message_id", "")

   
	payload := strings.NewReader(data.Encode())

	req, _ := http.NewRequest("POST", apiurl, payload)

	req.Header.Add("content-type", "application/x-www-form-urlencoded")

	res, _ := http.DefaultClient.Do(req)

	defer res.Body.Close()
	body, _ := ioutil.ReadAll(res.Body)

 
	fmt.Println(string(body))

}

Вы можете посмотреть полную документацию Whatsapp API здесь.