I'm trying to create my own Telegram bot as a project and don't want to use any of the libraries already out there that already do all the hard work for me as I want to do this for myself as on going self learning.
I'm using CPPRESTSDK and trying to get values from the JSON back from telegram.
Here is an example of the JSON
{
"ok": true,
"result": [
{
"update_id": 534699960,
"message": {
"message_id": 159183,
"from": {
"id": HIDDEN,
"is_bot": false,
"first_name": "Hawke",
"username": "XXXXXXXX"
},
"chat": {
"id": HIDDEN,
"title": "CHANNEL_NAME_HIDDEN",
"username": "HIDDEN",
"type": "supergroup"
},
"date": 1548427328,
"text": "Nope, at work"
}
}
]
}
I'm trying to read the text value but I'm getting Keyname not found when trying to access result. The above JSON is stored into a file once retrieving the JSON from telegram.
try {
string_t importFile = U("json.txt");
ifstream_t f(importFile);
stringstream_t s;
json::value v;
if (f) {
s << f.rdbuf();
f.close();
v = json::value::parse(s);
auto results_array = v.at(U("result")).as_array();
}
}
catch (web::json::json_exception& excep) {
std::cout << excep.what();
}
Related
I created a collection in Postman that should work as my API documentation. I know, that for every endpoint I can save example responses, that will be included in the documentation.
Now I would like to include a response schema as well, so that people see a general definition of the data types and structure of the response. In OpenApi this is possible within the "response" block like this:
"responses": {
"200": {
"description": "200 response",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/User"
}
}
}
}
}
...
"components": {
"schemas": {
"User": {
"title": "User Schema",
"type": "object",
"properties": {
"name": {
"type": "string"
}
}
}
}
}
Is there a similar way to do this in Postman as well? I searched the documentation for quite some time but could not find anything useful except for one line here that sounds like it should be possible:
Each collection / request listing indicates the method, required authorization type, URL, description, headers, request and response structures, and examples.
Description supports markdown language you can use below content :
# Schema:
```
"responses": {
"200": {
"description": "200 response",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/User"
}
}
}
}
}
...
"components": {
"schemas": {
"User": {
"title": "User Schema",
"type": "object",
"properties": {
"name": {
"type": "string"
}
}
}
}
}
```
output:
I am having an API in backend to return the full json (schema and options) for a AlpacaJS form. The content-type of the response is application/json. Following is a sample response,
{
"options": {
"fields": {
"students": {
"items": {
"type": "tablerow"
},
"type": "table"
}
},
"form": {
"buttons": {
"submit": {
"click": "function(){alert(\"sample\");}"
}
}
}
},
"schema": {
"properties": {
"students": {
"items": {
"properties": {
"name": {
"required": true,
"title": "Name",
"type": "string"
},
"contact-number": {
"title": "Age",
"type": "string"
}
},
"type": "object"
},
"type": "array"
}
},
"type": "object"
}
}
When I click on the Submit button, I get the following error in browser console,
Uncaught TypeError: t.call is not a function
I think the issue is that the function is considered as a string in the following section of the response.
"form": {
"buttons": {
"submit": {
"click": "function(){alert(\"sample\");}"
}
}
}
Is there a way in AlpacaJS to send a javascript function from backend, or is there a way to convert the function string to a javascript function in frontend?
In order to get that, you should transform the stringified function to a function by doing new Function('return ' + val)(); (beware this is a form of eval and eval is evil).
Here's a working fiddle for that.
Tell me if it didn't work for you.
Is it possible to read the following data below using boost?
{
"ok": true,
"result": [
{
"update_id": 1235285,
"message": {
"message_id": 2,
"from": {
"id": 3325446,
"is_bot": false,
"first_name": "Test",
"language_code": "en-PH"
},
"chat": {
"id": 12532541,
"first_name": "Test Account",
"type": "private"
},
"date": 152014521,
"text": "Test Message"
}
}
]
}
You could see the linked post in comment,
To summarize you can have like following to read from a file say mfile.json :
boost::property_tree::ptree pt;
boost::property_tree::read_json("myfile.json", pt);
print_contents( pt );
where print_contents is:
void print_contents( const boost::property_tree::ptree& pt)
{
using boost::property_tree::ptree;
for (const auto& x: pt )
{
std::cout << x.first << ": " << x.second.get_value<std::string>() << std::endl;
print_contents(x.second);
}
}
See Here
I could have closed it as duplicate, but looks like there was no "better" post for reading a json file
I have following ember request
this.store.createRecord('food_list',requestObj
).save().then((response) => {
console.log(response);
console.log(response.id); // This is working
console.log(response.food_list_code); //this does NOT work !!!!!
}
It will call an API and save a record to database and then returns following response.
{
"links": {
"self": "/api/food_list"
},
"data": {
"type": "",
"id": "da6b8615-3f4334-550544442",
"attributes": {
"food_list_date": "2013-02-14 23:35:19",
"food_list_id": "da6b8615-3f4334-550544442",
"food_list_code": "GORMA",
},
"relationships": {
"food_list_parameters": {
"data": [
{
"type": "food_list_parameter",
"id": "RERAFFASD9ASD09ASDFA0SDFASD"
}
]
},
"food_new_Name": {
"data": {
"type": "food_new_Name",
"id": "AKASDJFALSKDFKLSDF23W32KJ2L23"
}
}
},
"links": {
"self": "/api/BLAH/BLAH/BLAH"
}
}
}
but since above response is a JSONAPI in form of an ember object, I dont know how to parse it.
If I try to get response.id, I get the string da6b8615-3f4334-550544442
But how to get value for food_list_code in response block. Or how to iterate the response object to get "food_list_code" and "food_list_date" ?
The output for console.log(response) is as following ember class
Class {__ember1500143184544: "ember1198", store: Class, _internalModel: InternalModel, currentState...
I appreciate your help.
M.
My Postman response body is this:
{
"jobs": [
{
"id": "00d21be0",
"name": "IT Department"
},
{
"id": "h27da349",
"name": "Car Sales"
},
{
"id": "5d2db4125",
"name": "Grocery Clerk"
},
{
"id": "65cd0cc1d",
"name": "Accounting Department"
},
{
"id": "8462284587",
"name": "Nurse"
},
{
"id": "9fe2ff9ee4",
"name": "Astronaut"
},
{
"id": "f40cb44799",
"name": "Phone Operator"
},
{
"id": "f4e0483257",
"name": "Project Leader"
}
]
}
What I would like to do is parse this response and set an Environmental Variable for id's associated with Nurse, Astronaut, and Grocery Clerk. The rest of the info I do not need. I cannot use the array [number] because its not a guarantee they come in the same order on other systems.
Is JSON.stringify response body the way to go? How do I pull those values?
My Postman test so far is:
var jsonData = JSON.parse(responseBody);
postman.setEnvironmentalVariable("jobs", JSON.stringify(jsonData));
What you can do is parse your json body with a loop and, when you hit the names you want, you set a global variable with the corresponding id, like :
// get the response body
var jsonData = JSON.parse(responseBody);
// init counter
var loop_count = 0
for (count = 0; count < jsonData.jobs.length; count++)
{
if (jsonData.jobs[count].name == "Nurse")
{
var job_id = jsonData.jobs[count].id;
postman.setEnvironmentalVariable("env_nurse", job_id);
}
}
This is the principle.
You can do if/else for the different names you want to catch but I don't recommend it (use an array that you can expand easily).
Same thing for the environment variable name, instead of hard coding, try to generate it with respect to your element name.