Skip to content

User Adapter#

For specifing user-defined functions when a notification received use user_adapter files. In these files you should define notifications handlers (for example: write notification into database, send request to other microservice). Template file source/_user_adapter.cpp can be used by it's renaming. Handler examples for all notification type are available in file user_adapter_example.cpp in directory `examples.

The user adapter located in:

  • source/user_adapter.cpp

If you don't have required file, create it by renaming with removing underscore from source/_user_adapter.cpp file.

1. Updating User Adapter#

We will update include/user_adapter.h and source/_user_adapter.cpp files as new webhooks are released. If you encountered build error, which tells compiler could not find required functions from user_adapter, please add new functions from source/_user_adapter.cpp to yours source/user_adapter.cpp.

2. Description User Adapter#

User Adapter contains your handlers for incoming webhooks. It works according to the following algorithm:

  1. Request to the server is received by webhook class;

  2. webhook class creates Response object and transmits request body to the validator class;

  3. After validation, Response object transmits into UserAdapter handler, based on request's body webToken;

  4. User Adapter function returns true if error or false if no error. Based on this value, server will return 200 OK or 400 Bad Request status.

The structure of Response object (response.h):

struct Response {
    bool error = true; // true if incoming webhook failed to validate
    std::string typeWebhook = ""; // webhookType taken from request body
    std::string bodyStr = "";  // contains request body if error = false, otherwise contains validation error description
    nlohmann::json bodyJson = ""; //  body of incoming request
}
  1. UserAdapter function defines as:
    static bool onWebhookType(greenapi::Response& body);
    
  2. UserAdapter function example:

In this example, handler will be called by webhook with type IncomingMessageReceived. Using the structure Response above, you could check for validate of request (body.error), work with webhook json structure (body.bodyJson) or get access to webhook raw body (body.bodyStr).

bool UserAdapter::onIncomingMessageReceived(greenapi::Response& body) {
    // Every request contains typeWebhook. Requests are rejected, if no typeWebhook given.
    const auto typeWebhook = body.bodyJson["typeWebhook"];

    // If you encountered errors while hanlding, you should return true.
    // It will change response status to 400 Bad Request with immediate return of the HTTP request result
    // 
    // if (<error>) {
    //    return true;
    //}

    greenapi::Logger::Log("Received webhook: " + nlohmann::to_string(typeWebhook) + std::string(" with body: ") + body.bodyStr, "info");

    // Write your handler here:

    // Return false if no error, after this 200 OK response will be returned
    return false;
}