Come inviare un’API WhatsApp usando Dart

Introduzione

In questo snippet di codice, imparerete come inviare un’API WhatsApp utilizzando Dart, per inviare un messaggio di testo, modelli WhatsApp, media, pulsanti interattivi, elenchi, prodotti … ecc, l’account Alvochat è necessario per eseguire i seguenti codici. Create un account se non ne avete uno.

Inviare un modello utilizzando l’API di WhatsApp

import 'package:http/http.dart' as http;


var headers = {
  'Content-Type': 'application/x-www-form-urlencoded'
};
var request = http.Request('POST', Uri.parse('https://api.alvochat.com/instance1199/messages/template'));
request.bodyFields = '{
'token':'YourToken',
'to':'16315555555',
'name':'hello_world',
'language':'en_us',
'header':'',
'body':'',
'buttons':'',
'priority':''
}';
request.headers.addAll(headers);

http.StreamedResponse response = await request.send();

if (response.statusCode == 200) {
  print(await response.stream.bytesToString());
}
else {
  print(response.reasonPhrase);
}

Inviare un messaggio di testo utilizzando l’API di WhatsApp

import 'package:http/http.dart' as http;


var headers = {
  'Content-Type': 'application/x-www-form-urlencoded'
};
var request = http.Request('POST', Uri.parse('https://api.alvochat.com/instance1199/messages/chat'));
request.bodyFields = '{
'token':'YourToken',
'to':'16315555555',
'body':'WhatsApp API on alvochat.com works good',
'priority':'',
'preview_url':'',
'message_id':''
}';
request.headers.addAll(headers);

http.StreamedResponse response = await request.send();

if (response.statusCode == 200) {
  print(await response.stream.bytesToString());
}
else {
  print(response.reasonPhrase);
}

Invia immagine

import 'package:http/http.dart' as http;


var headers = {
  'Content-Type': 'application/x-www-form-urlencoded'
};
var request = http.Request('POST', Uri.parse('https://api.alvochat.com/instance1199/messages/image'));
request.bodyFields = '{
'token':'YourToken',
'to':'16315555555',
'image':'https://alvochat-example.s3-accelerate.amazonaws.com/image/1.jpeg',
'caption':'image caption',
'priority':'',
'message_id':''
}';
request.headers.addAll(headers);

http.StreamedResponse response = await request.send();

if (response.statusCode == 200) {
  print(await response.stream.bytesToString());
}
else {
  print(response.reasonPhrase);
}

Inviare l’audio

import 'package:http/http.dart' as http;


var headers = {
  'Content-Type': 'application/x-www-form-urlencoded'
};
var request = http.Request('POST', Uri.parse('https://api.alvochat.com/instance1199/messages/audio'));
request.bodyFields = '{
'token':'YourToken',
'to':'16315555555',
'audio':'https://alvochat-example.s3-accelerate.amazonaws.com/audio/1.mp3',
'priority':'',
'message_id':''
}';
request.headers.addAll(headers);

http.StreamedResponse response = await request.send();

if (response.statusCode == 200) {
  print(await response.stream.bytesToString());
}
else {
  print(response.reasonPhrase);
}

Invia il video

import 'package:http/http.dart' as http;


var headers = {
  'Content-Type': 'application/x-www-form-urlencoded'
};
var request = http.Request('POST', Uri.parse('https://api.alvochat.com/instance1199/messages/video'));
request.bodyFields = '{
'token':'YourToken',
'to':'16315555555',
'video':'https://alvochat-example.s3-accelerate.amazonaws.com/video/1.mp4',
'caption':'video caption',
'priority':'',
'message_id':''
}';
request.headers.addAll(headers);

http.StreamedResponse response = await request.send();

if (response.statusCode == 200) {
  print(await response.stream.bytesToString());
}
else {
  print(response.reasonPhrase);
}

Inviare il documento

import 'package:http/http.dart' as http;


var headers = {
  'Content-Type': 'application/x-www-form-urlencoded'
};
var request = http.Request('POST', Uri.parse('https://api.alvochat.com/instance1199/messages/document'));
request.bodyFields = '{
'token':'YourToken',
'to':'16315555555',
'document':'https://alvochat-example.s3-accelerate.amazonaws.com/document/1.pdf',
'filename':'',
'priority':'',
'message_id':''
}';
request.headers.addAll(headers);

http.StreamedResponse response = await request.send();

if (response.statusCode == 200) {
  print(await response.stream.bytesToString());
}
else {
  print(response.reasonPhrase);
}

Invia l’adesivo

import 'package:http/http.dart' as http;


var headers = {
  'Content-Type': 'application/x-www-form-urlencoded'
};
var request = http.Request('POST', Uri.parse('https://api.alvochat.com/instance1199/messages/sticker'));
request.bodyFields = '{
'token':'YourToken',
'to':'16315555555',
'sticker':'https://alvochat-example.s3-accelerate.amazonaws.com/sticker/1.webp',
'priority':'',
'message_id':''
}';
request.headers.addAll(headers);

http.StreamedResponse response = await request.send();

if (response.statusCode == 200) {
  print(await response.stream.bytesToString());
}
else {
  print(response.reasonPhrase);
}

Inviare il contatto

import 'package:http/http.dart' as http;


var headers = {
  'Content-Type': 'application/x-www-form-urlencoded'
};
var request = http.Request('POST', Uri.parse('https://api.alvochat.com/instance1199/messages/contact'));
request.bodyFields = '{
'token':'YourToken',
'to':'16315555555',
'contact':'',
'priority':'',
'message_id':''
}';
request.headers.addAll(headers);

http.StreamedResponse response = await request.send();

if (response.statusCode == 200) {
  print(await response.stream.bytesToString());
}
else {
  print(response.reasonPhrase);
}

Invia posizione

import 'package:http/http.dart' as http;


var headers = {
  'Content-Type': 'application/x-www-form-urlencoded'
};
var request = http.Request('POST', Uri.parse('https://api.alvochat.com/instance1199/messages/location'));
request.bodyFields = '{
'token':'YourToken',
'to':'16315555555',
'lat':'37.484296',
'lng':'-122.148703',
'address':'Menlo Park, California, United States',
'name':'Meta Headquarters',
'priority':'',
'message_id':''
}';
request.headers.addAll(headers);

http.StreamedResponse response = await request.send();

if (response.statusCode == 200) {
  print(await response.stream.bytesToString());
}
else {
  print(response.reasonPhrase);
}

Inviare l’elenco

import 'package:http/http.dart' as http;


var headers = {
  'Content-Type': 'application/x-www-form-urlencoded'
};
var request = http.Request('POST', Uri.parse('https://api.alvochat.com/instance1199/messages/list'));
request.bodyFields = '{
'token':'YourToken',
'to':'16315555555',
'header':'header',
'body':' please select one of the following options',
'footer':'footer',
'button':'options',
'sections':'option_1,option_2,option_3',
'priority':'',
'message_id':''
}';
request.headers.addAll(headers);

http.StreamedResponse response = await request.send();

if (response.statusCode == 200) {
  print(await response.stream.bytesToString());
}
else {
  print(response.reasonPhrase);
}

Pulsante di invio

import 'package:http/http.dart' as http;


var headers = {
  'Content-Type': 'application/x-www-form-urlencoded'
};
var request = http.Request('POST', Uri.parse('https://api.alvochat.com/instance1199/messages/button'));
request.bodyFields = '{
'token':'YourToken',
'to':'16315555555',
'header':'header',
'body':' please select one of the following options',
'footer':'footer',
'buttons':'option_1,option_2,option_3',
'priority':'',
'message_id':''
}';
request.headers.addAll(headers);

http.StreamedResponse response = await request.send();

if (response.statusCode == 200) {
  print(await response.stream.bytesToString());
}
else {
  print(response.reasonPhrase);
}

Inviare i prodotti

import 'package:http/http.dart' as http;


var headers = {
  'Content-Type': 'application/x-www-form-urlencoded'
};
var request = http.Request('POST', Uri.parse('https://api.alvochat.com/instance1199/messages/product'));
request.bodyFields = '{
'token':'YourToken',
'to':'16315555555',
'header':'header',
'body':'Hi , check out our new products',
'footer':'footer',
'catalog_id':'',
'product':'',
'priority':'',
'message_id':''
}';
request.headers.addAll(headers);

http.StreamedResponse response = await request.send();

if (response.statusCode == 200) {
  print(await response.stream.bytesToString());
}
else {
  print(response.reasonPhrase);
}

È possibile consultare la documentazione completa dell’API di Whatsapp qui.