AddContact#
ืืฉืืื ืืฉืืฉืช ืืืืกืคืช ืืกืคืจ ืืื ืฉื ืงืฉืจ, ืืื ืืืืกืคืช ืืกืคืจ ืืกืคืจ ืืืืคืื ืื ืฉื ืืืืฉืืจ ืืจืืฉื (ืืืืคืื ืื ืืื).
ืื ืืืืื ืืื ืื ืฉื ืงืฉืจ ืืืืืืกืืค ืืกืคืจ ืืืืคืื ืื ืฉื ืืืืฉืืจ ืืจืืฉื?
ืืืืืืกืืค ืืฉ ืื ืื ืื ืฉื ืงืฉืจ ืืฉืื, ืฉืืื ื ืืงืืฉืจ ืืกืคืจ ืืืืคืื ืื ืฉื ืืืืฉืืจ ืืจืืฉื ืืืจืืจืช ืืืื. ืื ื ืขืฉื ืืื ืืฉืคืจ ืืช ืืคืจืืืืช ืืืืงื ืขื ืืขืืจืช ืื ืฉื ืงืฉืจ ืืืืืืกืืค ืืื ืืืฉืืจืื ืฉืื ืื: ืื ืืจืืืื, iOS, Windows ืืืืื ืืจื ื. ืืืจืืจืช ืืืื, ืกื ืืจืื ืื ืฉื ืงืฉืจ ืืื ืกืคืจ ืืืืคืื ืื ืฉื ืืืืฉืืจ ืืจืืฉื ืืืื ืืืืืกืืค ืขืฉืื ืืืืืช ืืืฉืืช, ืืื ืขืืื ืืืคืขืื ืกื ืืจืื ืื ืฉื ืงืฉืจ ื-API ืืืคืขืืื ืื ืืืืฉืืจ ืืจืืฉื ืฉืื. ืืืืืข ื ืืกืฃ, ืขืืื ืืืืืจ ืืืฆื ืื ืื ืืช ืจืฉืืืช ืื ืฉื ืืงืฉืจ ืืกืคืจ ืืืืคืื ืื ืฉื ืืืคืื ืืืืืจ?
ืืงืฉื#
ืืื ืืืืกืืฃ ืืกืคืจ ืืื ืฉื ืืงืฉืจ, ืืฉ ืืืฆืข ืืงืฉื ืืืชืืืช:
{{apiUrl}}/waInstance{{idInstance}}/addContact/{{apiTokenInstance}}
ืืงืืืช ืคืจืืืจื ืืืงืฉื apiUrl, idInstance ื-apiTokenInstance, ืขืืื ืืกืขืืฃ ืืคื ื ืชืืืืช ืืขืืืื {:target="_blank"}.
ืคืจืืืจื ืืืงืฉื#
| ืคืจืืืจ | ืกืื | ืืืื | ืขืจื ืืจืืจืช ืืืื | ืชืืืืจ |
|---|---|---|---|---|
chatId | string | ืื | - | ืืกืคืจ ืืืคืื ืืคืืจืื ืฉื ืืืื ืฆ'ืื ืืืฉื. ืืืฉื "79876543210@c.us" |
firstName | string | ืื | - | ืฉื ืืืฉ ืืงืฉืจ |
lastName | string | ืื | - | ืฉื ืืืฉืคืื |
saveInAddressbook | boolean | No | true | ืืื ืืื ืืฉืืืจ ืืช ืืืฉ ืืงืฉืจ ืืคื ืงืก ืืืืคืื ืื ืืืืฉืืจ ืืจืืฉื |
ืืืืื ืืืืฃ ืืืงืฉื#
{
"chatId": "79876543210@c.us",
"firstName": "John",
"lastName": "Doe",
"saveInAddressbook": true
}
ืชืืืื#
ืฉืืืช ืืชืืืื#
| ืฉืื | ืกืื | ืชืืืืจ |
|---|---|---|
addContact | boolean | ืืืืืจ True, ืื ืืฉืืื ืืืฆืขื ืืื ืฉืืืืืช. ืืื ื ืงืืื ืืชืืืืช ืืืฃ ืขื ืฉืืืื. |
messsage | string | ืชืืืืจ ืืฉืืืื, ืื ืืฉืืื ืืืฆืขื ืขื ืฉืืืื. ืืื ื ืงืืื ืืชืืืืช ืืืฃ ืืื ืฉืืืื. |
ืืืืื ืืืืฃ ืืชืืืื#
{
"addContact": true
}
ืืืฆืืข ืืฉืืื ืขื ืฉืืืื#
status code 400 Bad request
{
"message": "Contact 77059640505@c.us already exists"
}
status code 404 Not found
{
"message": "77059640505@c.us is not on WhatsApp"
}
ืฉืืืืืช AddContact#
ืจืฉืืืช ืืฉืืืืืช ืืืฉืืชืคืืช ืืื ืืฉืืืืช ื ืืฆืืช ืืคืจืง ืฉืืืืืช ืกืื ืืจืืืืช
| ืงืื HTTP | ืืืื ืฉืืืื | ืชืืืืจ |
|---|---|---|
| 400 | Bad Request Contact 79876543210@c.us already exists | ืืืฉ ืงืฉืจ ืขื ืืืกืคืจ ืืื ืืืจ ืงืืื ืืคื ืงืก ืืืืคืื ืื |
| 404 | Not found 79876543210@c.us is not on WhatsApp | ืื ืงืืื ืืืฉ ืงืฉืจ ืขื ืืืกืคืจ ืืื ืืืืืืกืืค |
ืืืืืืืช ืงืื#
import requests
url = "{{apiUrl}}/waInstance{{idInstance}}/addContact/{{apiTokenInstance}}"
payload = {
"chatId": "79876543210@c.us",
"firstName": "John",
"lastName": "Doe",
"saveInAddressbook": true
}
headers = {
'Content-Type': 'application/json'
}
response = requests.post(url, json=payload, headers=headers)
print(response.text.encode('utf8'))
<?php
//The apiUrl, idInstance and apiTokenInstance values are available in console, double brackets must be removed
$url = '{{apiUrl}}/waInstance{{idInstance}}/addContact/{{apiTokenInstance}}';
//chatId is the number (@c.us)
$data = array(
'chatId' => '71234567890@c.us',
'firstName' => 'John',
'lastName' => 'Doe',
'saveInAddressbook' => 'true',
);
$options = array(
'http' => array(
'header' => "Content-Type: application/json\r\n",
'method' => 'POST',
'content' => json_encode($data)
)
);
$context = stream_context_create($options);
$response = file_get_contents($url, false, $context);
echo $response;
?>
curl --location '{{apiUrl}}/waInstance{{idInstance}}/addContact/{{apiTokenInstance}}' \
--header 'Content-Type: application/json' \
--data-raw '{
"chatId": "71234567890@c.us",
"firstName": "John",
"lastName": "Doe",
"saveInAddressbook": true
}'
Sub AddContact()
Dim URL As String
Dim RequestBody As String
Dim http As Object
' The apiUrl, idInstance and apiTokenInstance values are available in console, double brackets must be removed
URL = "{{apiUrl}}/waInstance{{idInstance}}/addContact/{{apiTokenInstance}}"
' chatId is the number (@c.us)
RequestBody = "{""chatId"":""71234567890@c.us"",""firstName"":""John"", ""lastName"":""Doe"", ""saveInAddressbook"":""true""}"
Set http = CreateObject("MSXML2.XMLHTTP")
With http
.Open "POST", URL, False
.setRequestHeader "Content-Type", "application/json"
.send RequestBody
End With
Set http = Nothing
End Sub
program addContact;
{$APPTYPE CONSOLE}
uses
System.SysUtils,
System.Classes, System.Net.HttpClient, System.Net.URLClient, System.Net.HttpClientComponent;
var
HttpClient: TNetHTTPClient;
RequestBody: TStringStream;
RequestHeaders: TNetHeaders;
Response: IHTTPResponse;
EndpointURL, ID_INSTANCE, API_TOKEN_INSTANCE: string;
begin
ID_INSTANCE := '110100001';
API_TOKEN_INSTANCE := 'd75b3a66374942c5b3c019c698abc2067e151558acbd451234';
EndpointURL := 'https://api.green-api.com/waInstance' + ID_INSTANCE + '/addContact/' + API_TOKEN_INSTANCE;
HttpClient := TNetHTTPClient.Create(nil);
RequestBody := TStringStream.Create('{ "chatId": "79876543210@c.us", "firstName": "John", "lastName": "Doe", "saveInAddressbook": true }', TEncoding.UTF8);
RequestHeaders := [
TNetHeader.Create('Content-Type', 'application/json')
];
try
Response := HTTPClient.Post(EndpointURL, RequestBody, nil, RequestHeaders);
if Response.StatusCode = 200 then
Writeln('[Response]: ' + Response.ContentAsString)
else
Writeln('[ERROR ' + IntToStr(Response.StatusCode) + ']:' + Response.StatusText + '' + Response.ContentAsString);
readln;
except
on E: Exception do
Writeln(E.ClassName, ': ', E.Message);
end;
HttpClient.Free;
RequestBody.Free;
end.