Skip to content

How to receive notifications#

Installing#

To build and install the library to your project use the instruction.

Import#

Specify the directory of GREEN-API library's header file:

#include "greenapi.hpp"

Examples#

How to initialize an object#

To initialize an object it is required to use apiUrl and mediaUrl parameters specifically for your instance from your console, that way you will get the most stable API work and minimal response time.

greenapi::GreenApi instance1101000001{
    "https://api.green-api.com",
    "https://media.green-api.com",
    "1101123456",
    "87be9e9532fc49748f2a44b9242e55f2e89f4bf97ed6498f80"
    };

How to receive notifications#

Link to the example: receiveIncomingNotifications.cpp.

while (1) {
    greenapi::Response receiveNotification = instance1101000001.receiving.receiveNotification(5);

    if (receiveNotification.error) {
        std::cout << "receiveNotification error: {status code: " << receiveNotification.status_code << ", request time: " << receiveNotification.total_time << ", body: " << receiveNotification.bodyStr << "}" << "\n" << std::endl;
    }
    else if(receiveNotification.bodyStr == "null") {
        std::cout << "All notifications received" << "\n" << std::endl;
        return 0;
    }
    else {
        std::cout << "\treceiptId: " << receiveNotification.bodyJson["receiptId"] << "\n" << std::endl;
        std::cout << "\tbody: " << receiveNotification.bodyJson["body"] << "\n" << std::endl;
    }

    greenapi::Response deleteNotification = instance1101000001.receiving.deleteNotification(receiveNotification.bodyJson["receiptId"]);

    if (deleteNotification.error) {
        std::cout << "deleteNotification error: {status code: " << deleteNotification.status_code << ", request time: " << deleteNotification.total_time << ", body: " << deleteNotification.bodyStr << "}" << "\n" << std::endl;
    }
    else {
        std::cout << "\tdeleteNotification result: " << deleteNotification.bodyJson["result"] << "\n" << std::endl;
    }
}

Pay attention that all the library's methods return structure of greenapi::Response type. To control the programm's work, check the method's execution, error field.

Examples list#

Description Link to the example
How to send message main.cpp
How to send file by upload sendFileByUpload.cpp
How to send file by URL sendFileByUrl.cpp
How to receive notification receiveIncomingNotifications.cpp
How to create group createGroupAndSendMessage.go