1

I'm unable to fetch and display the date from "http://worldtimeapi.org/api/timezone/Etc/GMT+7" but i'm able to display the date and time using the inbuilt function but i want to display it using the api.

function MessageHandler(context, event) {
  if (event.message == "date2") {
    context.simplehttp.makeGet('http://worldtimeapi.org/api/timezone/Etc/GMT+7', null, parser);
  }
}

function parser(context, event) {
  var dateJson = JSON.parse(event.getresp);
  var date = dateJson.date;
  context.sendResponse("Today's date is : " + date);
}

function MessageHandler(context, event) {
  if (event.message == "date") {
    context.sendResponse("Today's date is :" + Date());
  }
}

function EventHandler(context, event) {
  context.simpledb.roomleveldata = {};
  MessageHandler(context, event);
}

I'm using gupshup IDE, whenever I give the input date2 I'm getting these lines in bot log

2407: [2019-03-08T09:00:33.249] [INFO] default-Setting Up Bot Event For Bot=> /devnode/Displayinhthedate
2408:[2019-03-08T09:00:33.250] [INFO] default - Setting Up Bot Context For Bot =>/devnode/Displayinhthedate
2409:[2019-03-08T09:00:33.252] [INFO] default-Successfully connected to: /devnode/Displayinhthedate/temp_db
2410:[2019-03-08T09:00:33.285] [INFO] default-Successfully Fetched Data For Key=>bot:global
2411:[2019-03-08T09:00:33.291] [INFO] default-Successfully Fetched Data For Key =>room:1234

mplungjan
  • 169,008
  • 28
  • 173
  • 236
Nooruddin Saheb
  • 77
  • 1
  • 11
  • 1
    Any errors? Console error, server errors? – mplungjan Mar 08 '19 at 07:29
  • I'm using gupshup IDE, whenever I give the input date2 I'm getting this lines in bot log `2407: [2019-03-08T09:00:33.249] [INFO] default-Setting Up Bot Event For Bot=> /devnode/Displayinhthedate 2408:[2019-03-08T09:00:33.250] [INFO] default - Setting Up Bot Context For Bot =>/devnode/Displayinhthedate 2409:[2019-03-08T09:00:33.252] [INFO] default-Successfully connected to: /devnode/Displayinhthedate/temp_db 2410:[2019-03-08T09:00:33.285] [INFO] default-Successfully Fetched Data For Key=>bot:global 2411:[2019-03-08T09:00:33.291] [INFO] default-Successfully Fetched Data For Key =>room:1234` – Nooruddin Saheb Mar 08 '19 at 09:12

1 Answers1

1

The problem is here:

var dateJson = JSON.parse(event.getresp);
var date = dateJson.date;

you are getting the date as dateJson.date but there is no key with date in the JSON object.

{
  "week_number": "10",
  "utc_offset": "-07:00",
  "unixtime": "1552031359",
  "timezone": "Etc/GMT+7",
  "dst_until": null,
  "dst_from": null,
  "dst": false,
  "day_of_year": 67,
  "day_of_week": 5,
  "datetime": "2019-03-08T00:49:19.371885-07:00",
  "abbreviation": "-07"
}

The date you can get by using dateJson.datetime. Hope it will help.

mplungjan
  • 169,008
  • 28
  • 173
  • 236
Abdullah Aziz
  • 700
  • 5
  • 11
  • function parser(context, event) { var dateJson = JSON.parse(event.getresp); var date = dateJson.datetime; context.sendResponse("Today's date is : "+date); } I've made the suggested changes yet its not working – Nooruddin Saheb Mar 08 '19 at 08:56
  • JSON.parse will give you the dateTime property as a string. Do you need then to explicitly convert it to a Date object? – bbsimonbb Mar 08 '19 at 09:15
  • No just need to be able to fetch any of the property and display datetime is an example `function MessageHandler(context, event) { if(event.message== "date2") { context.simplehttp.makeGet('http://date.jsontest.com/',null,parser); } } function parser(context, event) { var dateJson = JSON.parse(event.getresp); var abbreviation = dateJson.abbreviation; context.sendResponse("Today's date is : "+abbreviation); }` this is the code i'm trying to run now but same problem exists. – Nooruddin Saheb Mar 08 '19 at 09:48
  • you have two functions with the same name: `MessageHandler`, please try to write single function with two conditional blocks on `event.message`. and run and also please share the error you are facing. and also try to `console.log(event.getresp)` and `consol.log(dateJson)` to see what is inside. – Abdullah Aziz Mar 10 '19 at 23:48