You are on page 1of 1

#include <iostream> #include <curl/curl.h> #include <jsoncpp/json/json.

h> // Remplacez
TOKEN_VALUE avec votre propre token Wit.ai const std :: string WIT_TOKEN =
"TOKEN_VALUE" ; // Fonction pour effectuer une requête HTTP GET std :: string http_get(const
std::string& url) { CURL* curl = curl_easy_init(); std :: string response; if (curl) { curl_easy_setopt(curl,
CURLOPT_URL, url.c_str()); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, []( char * ptr, size_t size,
size_t nmemb, void * userdata) -> size_t { (( std :: string *)userdata)->append(ptr, size * nmemb); return
size * nmemb; }); curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response); CURLcode res =
curl_easy_perform(curl); if (res != CURLE_OK) { std :: cerr << "Erreur lors de la requête HTTP GET: "
<< curl_easy_strerror(res) << std :: endl ; } curl_easy_cleanup(curl); } return response; } // Fonction pour
envoyer une requête à Wit.ai Json::Value wit_request(const std::string& query) { std :: string url =
"https://api.wit.ai/message?v=20220226&q=" ; url += curl_easy_escape(nullptr, query.c_str(),
query.length()); struct curl_slist* headers = nullptr; headers = curl_slist_append(headers, ( "Authorization:
Bearer " + WIT_TOKEN).c_str()); headers = curl_slist_append(headers, "Content-Type:
application/json" ); CURL* curl = curl_easy_init(); std :: string response; if (curl) { curl_easy_setopt(curl,
CURLOPT_URL, url.c_str()); curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, []( char * ptr, size_t size, size_t nmemb, void *
userdata) -> size_t { (( std :: string *)userdata)->append(ptr, size * nmemb); return size * nmemb; });
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response); CURLcode res = curl_easy_perform(curl); if (res
!= CURLE_OK) { std :: cerr << "Erreur lors de la requête HTTP GET: " << curl_easy_strerror(res) <<
std :: endl ; } curl_easy_cleanup(curl); } Json::Value json_response; Json::Reader reader; reader.parse(response,
json_response); return json_response; } int main() { std :: string user_input; std :: cout << "Bonjour,
comment puis-je vous aider?" << std :: endl ; while ( true ) { std ::getline( std :: cin , user_input);
Json::Value response = wit_request(user_input); if (!response.isNull()) { std :: string intent =
response[ "intents" ][ 0 ][ "name" ].asString();

You might also like