SendMessage#
The method is aimed for sending a text message to a personal or a group chat. The message will be added to the send queue. Linked device not required when sending. Messages will be kept for 24 hours in the queue until account will be authorized The rate at which messages are sent from the queue is managed by Message sending delay parameter.
Request#
To send a text message, you have to execute a request at:
POST https://api.green-api.com/waInstance{{idInstance}}/sendMessage/{{apiTokenInstance}}
For idInstance
and apiTokenInstance
request parameters, refer to Before you start section.
Request parameters#
Parameter | Type | Mandatory | Description |
---|---|---|---|
chatId | string | Yes | Chat Id |
message | string | Yes | Message text. Emoji 😃 characters supported |
quotedMessageId | string | No | Quoted message ID. If present, the message will be sent quoting the specified chat message |
archiveChat | boolean | No | Archives the chat to which the message was sent. Takes value: true |
linkPreview | boolean | No | The parameter includes displaying a preview and a description of the link. Enabled by default. Accepts values: true/false |
The maximum length of a text message is 4096 characters
Request body example#
Sending a message to a personal chat:
{
"chatId": "11001234567@c.us",
"message": "I use Green-API to send this message to you!"
}
Sending a message to a group chat:
{
"chatId": "11001234567-1581234048@g.us",
"message": "I use Green-API to send this message to you!"
}
Sending a quoted message:
{
"chatId": "11001234567@с.us",
"message": "I use Green-API to send this message to you!",
"quotedMessageId": "361B0E63F2FDF95903B6A9C9A102F34B"
}
Response#
Response parameters#
Parameter | Type | Description |
---|---|---|
idMessage | string | Sent message Id |
Response body example#
{
"idMessage": "3EB0C767D097B7C7C030"
}
SendMessage errors#
For a list of errors common to all methods, refer to Common errors section
Request examples#
import requests
url = "https://api.green-api.com/waInstance{{idInstance}}/sendMessage/{{apiTokenInstance}}"
payload = "{\r\n\t\"chatId\": \"11001234567@c.us\",\r\n\t\"message\": \"I use Green-API to send this message to you!\"\r\n}"
headers = {
'Content-Type': 'application/json'
}
response = requests.request("POST", url, headers=headers, data = payload)
print(response.text.encode('utf8'))
<?php
//The idInstance and apiTokenInstance values are available in your account, double brackets must be removed
$url = 'https://api.green-api.com/waInstance{{idInstance}}/sendMessage/{{apiTokenInstance}}';
//chatId is the number to send the message to (@c.us for private chats, @g.us for group chats)
$data = array(
'chatId' => '71234567890@c.us',
'message' => 'Hello World'
);
$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 'https://api.green-api.com/waInstance{{idInstance}}/sendMessage/{{apiTokenInstance}}' \
--header 'Content-Type: application/json' \
--data-raw '{
"chatId": "71234567890@c.us",
"message": "Hello World"
}'
Sub SendMessage()
Dim URL As String
Dim RequestBody As String
Dim http As Object
' The idInstance and apiTokenInstance values are available in your account, double brackets must be removed
URL = "https://api.green-api.com/waInstance{{idInstance}}/sendMessage/{{apiTokenInstance}}"
' chatId is the number to send the message to (@c.us for private chats, @g.us for group chats)
RequestBody = "{""chatId"":""71234567890@c.us"",""message"":""Hello World""}"
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 sendMessage;
{$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 + '/sendMessage/' + API_TOKEN_INSTANCE;
HttpClient := TNetHTTPClient.Create(nil);
RequestBody := TStringStream.Create('{ "chatId": "71234567890@c.us", "message": "test" }', 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.