SendMessage#
The method is intended for sending a text message to a chat.
The message is added to the sending queue.
The rate at which messages are sent from the queue is set by the message sending interval.
The method uses limits on the request rate per second.
Request#
To send a text message, you need to execute a request at:
{{apiUrl}}/waInstance{{idInstance}}/sendMessage/{{apiTokenInstance}}
To get the apiUrl, idInstance and apiTokenInstance request parameters, refer to the Before you start section.
Request parameters#
| Parameter | Type | Required | Description |
|---|---|---|---|
chatId | string | Yes | Chat identifier |
message | string | Yes | Message text. Emoji 😃 characters are supported. The maximum length of a text message is 4096 characters. |
quotedMessageId | string | No | Identifier of the quoted message |
typingTime | integer | No | The time the typing notification is displayed in the correspondent's chat. Takes a value in the range from 1000 to 20000 milliseconds (from 1 to 20 seconds) |
typingType | string | No | Typing type. Possible values: |
text - "typing..." (default) | |||
record_voice_note - "recording a voice message..." | |||
upload_voice_note - "sending a voice message..." | |||
record_video_note - "recording a video message..". | |||
upload_video_note - "sending a video message..." | |||
record_video - "recording a video..." | |||
upload_video - "sending a video..." | |||
upload_photo - "sending a photo..." | |||
upload_document - "sending a file..." | |||
choose_sticker - "choosing a sticker..." | |||
choose_location - "choosing a location..." | |||
choose_contact - "choosing a contact..." |
Request body example#
Sending a message to a personal chat#
{
"chatId": "10000000",
"message": "I use GREEN-API to send this message!"
}
Sending a message by phone number#
{
"chatId": "79876543210@c.us",
"message": "I use GREEN-API to send this message!"
}
Sending a message to a group chat#
{
"chatId": "-10000000000000",
"message": "I use GREEN-API to send this message!"
}
Sending a message with quoting#
{
"chatId": "10000000",
"message": "I use Green-API to send this message to you!",
"quotedMessageId": "1769676078000"
}
Response#
Response fields#
| Field | Type | Description |
|---|---|---|
idMessage | string | Identifier of the sent message |
Response body example#
{
"idMessage": "1769676078000"
}
Errors#
For the list of errors common to all methods, see the Common errors section
| HTTP code | Error identifier | Description |
|---|---|---|
| 400 | Bad Request Validation failed | Validation error |
| 400 | Validation failed. Details: 'message' length must be less than or equal to 4096 characters long | The message text must be less than or equal to 4096 characters |
Sending to a service chat
Sending to a service chat (for example, "Service notifications") will result in an error. The system will return code 200 and a message id, but the message will not be sent, and an outgoingMessageStatus webhook with "status": "failed" will arrive for it.
Code examples#
import requests
url = "{{apiUrl}}/waInstance{{idInstance}}/sendMessage/{{apiTokenInstance}}"
payload = {
"chatId": "10000000",
"message": "I use GREEN-API to send this message!"
}
headers = {'Content-Type': 'application/json'}
response = requests.post(url, json=payload, headers=headers)
print(response.text.encode('utf8'))
<?php
$url = '{{apiUrl}}/waInstance{{idInstance}}/sendMessage/{{apiTokenInstance}}';
$data = array(
'chatId' => '10000000',
'message' => 'I use GREEN-API to send this message!'
);
$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}}/sendMessage/{{apiTokenInstance}}' \
--header 'Content-Type: application/json' \
--data-raw '{
"chatId": "10000000",
"message": "I use GREEN-API to send this message!"
}'
var restTemplate = new RestTemplate();
var requestUrl = new StringBuilder();
requestUrl
.append({{apiUrl}})
.append("/waInstance").append({{idInstance}})
.append("/sendMessage/")
.append({{apiTokenInstance}});
var headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
var jsonBody = "{\r\n\t\"chatId\": \"10000000\",\r\n\t\"message\": \"I use GREEN-API to send this message!\"\r\n}";
var requestEntity = new HttpEntity<>(jsonBody, headers);
var response = restTemplate.exchange(requestUrl.toString(), HttpMethod.POST, requestEntity, String.class);
System.out.println(response);
var requestUrl = new StringBuilder();
requestUrl
.append({{apiUrl}})
.append("/waInstance").append({{idInstance}})
.append("/sendMessage/")
.append({{apiTokenInstance}});
var response = Unirest.post(requestUrl.toString())
.header("Content-Type", "application/json")
.body("{\r\n\t\"chatId\": \"10000000\",\r\n\t\"message\": \"I use GREEN-API to send this message!\"\r\n}")
.asString();
System.out.println(response);
Sub SendMessage()
Dim URL As String
Dim RequestBody As String
Dim http As Object
URL = "{{apiUrl}}/waInstance{{idInstance}}/sendMessage/{{apiTokenInstance}}"
RequestBody = "{""chatId"":""10000000"",""message"":""I use GREEN-API to send this message!""}"
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": "10000000", "message": "I use GREEN-API to send this message!" }', 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.
Emoji 😃 characters#
There are several ways to send a smiley or other characters through the API:
1. With hotkeys#
The easiest way to do this is to use the emoji built into your operating system.
To do this, place the cursor in the text field and use the hotkeys:
- For Windows:
Win + . - For MacOS:
control + command + space
Then select the character you are interested in in the pop-up window.
Unfortunately, this method does not work everywhere.
2. By copying#
Sometimes the simplest solution is to copy an emoji from a messenger, a browser or any other source.
Example: 😃😎🤯
An example site with emoji.
3. Using character codes#
If the field does not support pasting, or the pasted smileys are displayed incorrectly, try using character encoding.
To send smileys, we recommend using UTF-16. To do this, you need to represent the character as a hexadecimal number or a surrogate pair, two hexadecimal numbers.
\uD83D\uDE01 - an example of a surrogate pair
These numbers are in the Unicode character table.