How to get message sent date in webhook whatsapp API - webhooks

how to get message sent date. The timestamp of the message that arrives in the whatsapp API webhook is the date on which it arrives. I need to retrieve date the user sent. It's possible?
In the message model I receive, in this format in the webhook, the timestamp is the time it arrives on the server. I need to retrieve the time the user sent the message, in case he is offline
{
"object": "whatsapp_business_account",
"entry": [
{
"id": "8856996819413533",
"changes": [
{
"value": {
"messaging_product": "whatsapp",
"metadata": {
"display_phone_number": "16505553333",
"phone_number_id": "27681414235104944"
},
"contacts": [
{
"profile": {
"name": "Kerry Fisher"
},
"wa_id": "16315551234"
}
],
"messages": [
{
"from": "16315551234",
"id": "wamid.ABGGFlCGg0cvAgo-sJQh43L5Pe4W",
"timestamp": "1603059201",
"text": {
"body": "Hello this is an answer"
},
"type": "text"
}
]
},
"field": "messages"
}
]
}
]
}
I've tried sending other types of messages, and even monitoring all dates that are sent to the webhook. But none match the user's upload date

Currently, there is only one timestamp in message payload, that is,
The time when the WhatsApp server received the message from the customer.
https://developers.facebook.com/docs/whatsapp/cloud-api/webhooks/components#messages-object

Related

Google chat custom cards using dialogflow fulfilment webhook

I am trying to integrate DialogFlow bot with Hangouts Chat (for G Suite). I have enabled the integration on DialogFlow and the basic intents are working fine.
In order to perform backend operations using fulfillment, I have created a firebase cloud function and added this as the webhook URL on DialogFlow fulfillment page.
I have written the cloud function code to identify the intent, and to generate the Webhook response format for a simple text response. This is working, and I am seeing the firestore data being modified in response to the intent.
However for a more complicated intent, I wish to use more of the dynamic card based response that Chat offers. In order to achieve this, I have looked at the documentation for dialogflow card response.
I saw this following code at https://cloud.google.com/dialogflow/docs/integrations/hangouts. When I paste this into the dialogflow intent editor UI under hangouts custom payload (after disabling webhook integration), it works
{
"hangouts": {
"header": {
"title": "Pizza Bot Customer Support",
"subtitle": "pizzabot#example.com",
"imageUrl": "..."
},
"sections": [{
"widgets": [{
"keyValue": {
"icon": "TRAIN",
"topLabel": "Order No.",
"content": "12345"
}
},
{
"keyValue": {
"topLabel": "Status",
"content": "In Delivery"
}
}]
},
{
"header": "Location",
"widgets": [{
"image": {
"imageUrl": "https://dummyimage.com/600x400/000/fff"
}
}]
},
{
"header": "Buttons - i could leave the header out",
"widgets": [{
"buttons": [{
"textButton": {
"text": "OPEN ORDER",
"onClick": {
"openLink": {
"url": "https://example.com/orders/..."
}
}
}
}]
}]
}]
}
}
This is exactly what I need, but I need this response from the webhook. I'm not getting the correct response format to map between the two.
When I try to integrate the same code with the webhook, I am not getting any reply on hangouts chat. When I check the history section on dialogflow UI, here is the response structure as mentioned in Raw interaction log
{
"queryText": "<redacted>",
"parameters": {},
"intent": {
"id": "<redacted>",
"displayName": "<redacted>",
"priority": 500000,
"webhookState": "WEBHOOK_STATE_ENABLED"
},
"intentDetectionConfidence": 1,
"diagnosticInfo": {
"webhook_latency_ms": 284
},
"languageCode": "en",
"slotfillingMetadata": {
"allRequiredParamsPresent": true
},
"id": "<redacted>",
"sessionId": "<redacted>",
"timestamp": "2020-07-30T12:05:29.094Z",
"source": "agent",
"webhookStatus": {
"webhookUsed": true,
"webhookPayload": {
"hangouts": {
"header": {
"subtitle": "pizzabot#example.com",
"title": "Pizza Bot Customer Support",
"imageUrl": "..."
},
"sections": [
{
"widgets": [
{
"keyValue": {
"content": "12345",
"topLabel": "Order No.",
"icon": "TRAIN"
}
},
{
"keyValue": {
"topLabel": "Status",
"content": "In Delivery"
}
}
]
},
{
"widgets": [
{
"image": {
"imageUrl": "https://dummyimage.com/600x400/000/fff"
}
}
],
"header": "Location"
},
{
"widgets": [
{
"buttons": [
{
"textButton": {
"text": "OPEN ORDER",
"onClick": {
"openLink": {
"url": "https://example.com/orders/..."
}
}
}
}
]
}
],
"header": "Buttons - i could leave the header out"
}
]
}
},
"webhookStatus": {
"message": "Webhook execution successful"
}
},
"agentEnvironmentId": {
"agentId": "<redacted>",
"cloudProjectId": "<redacted>"
}
}
I also found this link on chat docs which explains how to show an interactive card based UI https://developers.google.com/hangouts/chat/how-tos/cards-onclick. However I'm not able to understand how to integrate the same with the webhook.
UPDATE
I have followed a tutorial at https://www.leeboonstra.com/Bots/custom-payloads-rich-cards-dialogflow/ and was able to get the card response to show up using the sample code they mention. It is using this deprecated library (https://github.com/dialogflow/dialogflow-fulfillment-nodejs). Here is the code for that to work,
let payload = new Payload("hangouts", json, {
rawPayload: true,
sendAsMessage: true,
});
agent.add(payload);
Here the json variable should be the previous JSON structure I have mentioned. So now, I'm able to map to the correct response format using the deprecated API. However, I'm not able to get the button to send the right response to the back end. Here is the buttons field that I modified from the previous json,
"buttons": [
{
"textButton": {
"text": "Click Me",
"onClick": {
"action": {
"actionMethodName": "snooze",
"parameters": [
{
"key": "time",
"value": "1 day"
},
{
"key": "id",
"value": "123456"
}
]
}
}
}
}
]
As far as I know, responding to a Google Chat (formerly Hangouts Chat) button isn't possible when using the direct Dialogflow integration.
The problem is that the button response can be sent one of two ways:
An event will be sent back to the bot code indicating the click.
Using the onClick.openLink.url property, as most of your test show.
This will take the person clicking it to the URL in question. But once there, you're taken out of the bot flow.
However, the documentation for the Hangouts Chat integration with Dialogflow doesn't provide any information about how this event is passed to Dialogflow, and the last time I tested it - it isn't.
You can write your own integration using Google Chat's API on something like Cloud Functions or Apps Script and have your script call Dialogflow's Detect Intent API to determine what Intent would be triggered by the user (and determine replies or call the webhook for additional processing). Under this scheme, you can choose how to handle the onClick event. Making your own integration also provides you a way to do Incoming Webhooks, which isn't possible when using the Dialogflow integration.

Chatbase send multiple messages at once error sequence

I'm using https://chatbase.com/api/messages Chatbase API to send multiple messages to the Chatbase. The documentation stats that the response would provide error and success for individual messages that are sent in the request.
How I should detect which message is failed out of the 3 messages that I have provided in the request if the response provides error for one message?
JSON Request body for sending multiple messages
{
"messages": [
{
"api_key": "<API KEY>",
"type": "agent",
"user_id": "User_1",
"time_stamp": 1542895834,
"platform": "XXX",
"message": "Test 1",
"not_handled": false,
"version": "1.0",
"session_id": "session-User_1"
},
{
"api_key": "<API KEY>",
"type": "agent",
"user_id": "User_1",
"time_stamp": 1542895834,
"platform": "XXX",
"message": "Test 2",
"not_handled": false,
"version": "1.0",
"session_id": "session-User_1"
},
{
"api_key": "<API KEY>",
"type": "agent",
"user_id": "User_1",
"time_stamp": 1542895834,
"platform": "XXX",
"message": "Test 3",
"not_handled": false,
"version": "1.0",
"session_id": "session-User_1"
}
]
}
JSON Response body
{
"all_succeeded": false,
"responses": [
{
"error": "Error fetching parameter 'type': Invalid conversation type [dfg]",
"status": "error"
},
{
"message_id": 139429278,
"status": "success"
},
{
"error": "Error fetching parameter 'time_stamp': Received a time (1921-02-09 09:49:26) which was too small. Please send a time within the past day for metrics to appear in the dashboards, or omit the time_stamp field for the time to automatically be set to now.",
"status": "error"
}
],
"status": 200
}
The order of the error messages will correspond to the order of the messages you submitted. In this case, it was the first and third messages. I also noticed that you are not using unix milliseconds in your timestamps. The messages will need to be at least one millisecond apart in order to sort them correctly in the Chatbase reports.

How to detect a message has inline button telegram bot

I created a group controller bot in telegram.
Now i have a problem: Can i detect messages that contains inline button?
Standard Telegram API doesn't have a method for detecting these messages.
Whats the solution?
Messages which are triggered by an inline keyboard will have the key callback_query.
This is the structure you will receive.
{
"update_id": 123456789,
"callback_query": {
"id": "123456789123456789",
"from": { ... },
"message": {
"message_id": 123456789,
"from": { ... },
"chat": { ... },
"date": 123456789,
"text": "Message text"
},
"chat_instance": "123456789123456789",
"data": "your_callback_data"
}
}
Telegram Bot API CallbackQuery

Fulfillment response for Dialog flow

Just started with the DialogFlow building an app. I have hosted a service in Java on cloud (not using the firebase). Basically, I receive the data from the agent and send the response back as Json. For simple query its working as expected. Like if I say "My name is X", the service will respond as "Hello X" and it will be played on the Response. The JSON response is sent as
{speech: "Hello X", type:"0"}
Now, I want to fetch the user location, so I want to ask the user the permission to access the location. I have a separate intent that does not have any training_Phrases. It has an Event actions_intent_PERMISSION.
I am sending the following response
{
"conversationToken": "[\"_actions_on_google_\"]",
"expectUserResponse": true,
"expectedInputs": [
{
"inputPrompt": {
"richInitialPrompt": {
"items": [
{
"simpleResponse": {
"textToSpeech": "PLACEHOLDER_FOR_PERMISSION"
}
}
]
}
},
"possibleIntents": [
{
"intent": "actions.intent.PERMISSION",
"inputValueData": {
"#type": "type.googleapis.com/google.actions.v2.PermissionValueSpec",
"optContext": "To locate you",
"permissions": [
"NAME"
]
}
}
],
"speechBiasingHints": [
"$geo_city",
"$event_category",
"$event_date"
]
}
],
"responseMetadata": {
"status": {},
"queryMatchInfo": {
"queryMatched": true,
"intent": "1ec64dc5-a6f4-44f6-8483-633b8638c729"
}
}
}
But I am getting the response as 400 Bad request. Is there anything that I am doing wrong here or am I missing any thing?
There are three issues.
The first is that the actions_intent_PERMISSION event is sent in response to a permission request. So this should not be the intent that triggers the request.
Second, you're asking for the users name, but not their location. You want either the DEVICE_COARSE_LOCATION or DEVICE_PRECISE_LOCATION.
The third, and much bigger, issue is that the JSON you're sending is the format used by the Action SDK. Since you're using Dialogflow, you'll be using a different response format which is the basic Dialogflow response, plus Actions on Google specific content in the data.google JSON property.
Your response should look something more like this:
{
"data": {
"google": {
"expectUserResponse": true,
"systemIntent": {
"intent": "actions.intent.PERMISSION",
"data": {
"#type": "type.googleapis.com/google.actions.v2.PermissionValueSpec",
"optContext": "To locate you",
"permissions": [
"NAME",
"DEVICE_PRECISE_LOCATION"
]
}
}
}
}
}
Dialogflow also has some other examples of requests and replies that should help for the other parts of your conversation.

How to get Image sent by user in facebook messenger through api.ai?

Anyone know how to retrieve an image sent by user, via api.ai? Do I just access the original body request? How do I do that when it does not seem to be sending to api.ai? Do I just use my server to respond?
API.AI includes information on the original request made to API.AI from the 1-click integrations in the originalRequest JSON attribute sent with every API.AI webhook request. The format for a Facebook Messenger Rich Messaging message sent with an images has the form:
{
"originalRequest": {
"source": "facebook",
"data": {
"sender": {
"id": "<PSID>"
},
"recipient": {
"id": "<PAGE_ID>"
},
"timestamp": 1458692752478,
"message": {
"mid": "mid.145869661...",
"attachments": [
{
"type": "image",
"payload": {
"url": "<IMAGE_URL>"
}
}
]
}
}
}
}
Gist of full code example here
You can use the originalRequest.data.message.attachments[0].payload.url attribute to get the URL of the image.

Resources