Dialogflow Messenger Events Management - event-handling

I'm testing the dialogflow messenger events, and I am able to capture them but the result I should get according to the documentation (https://cloud.google.com/dialogflow/docs/integrations/dialogflow-messenger) for example when I click a list elemnent should be:
element: {
title: string,
subtitle: string,
image: {
src: {rawUrl}
},
event: {
name: string,
parameters: {},
languageCode: string
},
payload: {}
}
But I get this in the event:
{"isTrusted":false}
this is the way as I call it:
dfMessenger.addEventListener('df-list-element-clicked', function (event) {
// Handle event
console.log("df-list-element-clicked:" + JSON.stringify(event));
});
Is there another way to get the information of the event?

You can use event.detail.<<element_struction_for_respective_event>> to get the desired values from event listners.
For eg: For a chip click event listner (https://cloud.google.com/dialogflow/es/docs/integrations/dialogflow-messenger#df-chip-clicked)
use foll code: dfMessenger.addEventListener('df-chip-clicked', function (event) {
console.log("df chip-> "+ event.detail.query)
});
Same event.details.<<element_structure>> can be used for different events as per your scenario.

Related

How can I get the chat room online members without reload the page in converse js

I am using converse.js and I am trying to get the users who joined the chat room, I am able to get the users but when a new user joins I can not get the new user on my console log until I reload the page, Below I have created a plugin for getting the users.
export const moderationActions = () => {
window.converse.plugins.add('moderation-actions', {
dependencies: [],
initialize: function () {
const _converse = this._converse;
_converse.api.listen.on(
'getToolbarButtons',
async (toolbar_el: any, buttons: any) => {
toolbar_el.model.occupants.models.map((occupant: any) => {
console.log(occupant.get('nick'), occupant.get('show')),
console.log(occupant);
});
},
);
},
});
};
There are a few events related to users like membersFetched but don't know how can I get the users without reloading the page
Whenever someone joins or leaves a room, a new occupant model is added or removed from the occupants collection on the room model (available via the .occupants attribute).
Whenever a model is added or removed from a collection, an add or remove event is triggered which you can listen to.
So you can try something like this:
window.converse.plugins.add('num-occupants', {
initialize: function () {
const _converse = this._converse;
const room = await api.rooms.get('jid');
let num_occupants = room.occupants.length;
room.occupants.on('add', () => (num_occupants = room.occupants.length));
room.occupants.on('remove', () => (num_occupants = room.occupants.length));
}
});

How to prevent Local Notifications in Capacitor JS from opening the application?

On Android, I'm utilizing the LocalNotifications API in an Ionic project with Capacitor JS (https://capacitorjs.com/docs/apis/local-notifications). So the user can respond directly to a notification, I've added some action types to the payload like this:
LocalNotifications.registerActionTypes({
types: [
{
id: "workout-1",
actions: [
{
id: ":+1:",
title: "👍",
destructive: true,
},
{
id: ":muscle:",
title: "💪",
destructive: true,
},
{
id: "free_text",
title: "Respond",
input: true,
},
],
},
This enables the message to render like this:
However, even when tapping one of emojis, it opens the app. All I'd like it to do is trigger action performed on the emoji that was tapped. It works this way on iOS. Any ideas?
You have to set up an event listener in your application setup phase:
LocalNotifications.addListener('localNotificationActionPerformed', (notification) => {
const tappedActionId = notification.actionId;
// TODO: do action depending on actionId
}
When user taps on notification or any action, app will open and this function will run. When user taps on notification (not action) actionId = 'tap'.

implement Push Notifications in Ionic 2 with the Pub/Sub Model

Hi this is a duplicate of the question at
Push Notifications in Ionic 2 with the Pub/Sub Model
i have already implemented push notifications following this article >
https://medium.com/#ankushaggarwal/push-notifications-in-ionic-2-658461108c59#.xvoeao59a
what i want is to be able to send notifications to users when some events take place in the app like chat or booking or new job post.
how to go further , this is my first app.
NOTE: code is almost exactly the same as the tutorial, Java has only been converted to Kotlin
This is my acutal ionic side code (on login page). The push.on('registration') will be fired when the user opens the app, the variable this.device_id will later (on succesfull login) be sent to my Kotlin REST API so I know the device_id and have coupled it to a user. This way you can send targeted push notifications.
If you send a push notification from Kotlin (code shown below, looks a bit like Java), the (always open, even opens after startup) connection to Google will send your device (defined by the device_id a message with the notification data (title, message, etc.) after which your device will recognize the senderID and match it to use your ionic application.
initializeApp() {
this.platform.ready().then(() => {
let push = Push.init({
android: {
senderID: "1234567890"
},
ios: {
alert: "true",
badge: false,
sound: "true"
},
windows: {}
});
//TODO - after login
push.on('registration', (data) => {
this.device_id = data.registrationId;
});
push.on('notification', (data) => {
console.log('message', data.message);
let self = this;
//if user using app and push notification comes
if (data.additionalData.foreground) {
// if application open, show popup
let confirmAlert = this.alertCtrl.create({
title: data.title,
message: data.message,
buttons: [{
text: 'Negeer',
role: 'cancel'
}, {
text: 'Bekijk',
handler: () => {
//TODO: Your logic here
this.navCtrl.setRoot(EventsPage, {message: data.message});
}
}]
});
confirmAlert.present();
} else {
//if user NOT using app and push notification comes
//TODO: Your logic on click of push notification directly
this.navCtrl.setRoot(EventsPage, {message: data.message});
console.log("Push notification clicked");
}
});
push.on('error', (e) => {
console.log(e.message);
});
});
}
Kotlin code (converted from the Java example, basically the same
package mycompany.rest.controller
import mycompany.rest.domain.User
import java.io.OutputStream
import java.net.HttpURLConnection
import java.net.URL
class PushNotification {
companion object {
val SERVER_KEY = "sOmE_w31rD_F1r3Ba5E-KEy";
#JvmStatic fun sendPush(user: User, message: String, title: String) {
if(user.deviceId != "unknown"){
val pushMessage = "{\"data\":{\"title\":\"" +
title +
"\",\"message\":\"" +
message +
"\"},\"to\":\"" +
user.deviceId +
"\"}";
val url: URL = URL("https://fcm.googleapis.com/fcm/send")
val conn: HttpURLConnection = url.openConnection() as HttpURLConnection
conn.setRequestProperty("Authorization", "key=" + SERVER_KEY)
conn.setRequestProperty("Content-Type", "application/json")
conn.setRequestMethod("POST")
conn.setDoOutput(true)
//send the message content
val outputStream: OutputStream = conn.getOutputStream()
outputStream.write(pushMessage.toByteArray())
println(conn.responseCode)
println(conn.responseMessage)
}else {
println("Nope, not executed")
}
}
#JvmStatic fun sendPush(users: List<User>, message: String, title: String) {
for(u in users) {
PushNotification.sendPush(u, message, title)
}
}
}
}
Then the method can be called as PushNotification.sendPush(user1, "Hello world!", "my title");
(btw realized you won't need to run the pushnotification from a server (localhost/external). You can just create a main class which sends it with your hardcoded deviceId for testing purposes.

Chaining Controller Actions to one Route

I'm not sure what I am doing wrong here. I'm following this example here: https://gist.github.com/mikermcneil/5737561.
I want to chain two controller actions to one 'get' route. I need to return some json data and also a view. When attempting to send the data with the view I get an socket.io error message that says I can't send information through views.
'get /users': [
{
controller: 'UserController',
action: 'users'
},
{
controller: 'UserController',
action: 'getuser'
}
],
Right now to make it work I have to have two 'get' routes. One that returns my view ejs file and another that the socket can connect too.
var socket = io.connect('http://localhost:1337');
socket.get('/user', {}, function(users) {
var grid = new Slick.Grid("#userGrid", users.users, columns, options);
});
This works but then I have a random /user route out there that anyone can access that just returns json. I'd like to not have random routes that can be accessed through the browser if possible. So can I chain controller actions to one route? Or is there a way to return a res.view and json data in one action?
users: function(req, res, next) {
//User.find().populateAll().exec(function(err, users){
res.view('user/users');
next();
// res.json({
// success: true,
// users: users
// });
//});
},
//get user data and send it as json to for sockets
getuser: function (req, res) {
User.find().populateAll().exec(function(err, users) {
res.json({users: users});
});
},
I tried using next() and without. Thanks for any help!

Jeditable CANCEL callback from AJAX callback?

I see some answers for the Jeditable plugin to use a callback function from AJAX using complete callback function.
I know that the Jeditable has a callback function for the SUBMIT button, so I would like to know if there is a way to have a callback for the CANCEL button? I haven't found on the plugin docs.
Thanks for reply,
Carlos
PD. This is the source I see for COMPLETE from AJAX callback:
$("#editable_text").editable(submitEdit, {
indicator : "Saving...",
tooltip : "Click to edit...",
name : "Editable.FieldName",
id : "elementid",
type : "text",
});
function submitEdit(value, settings)
{
var edits = new Object();
var origvalue = this.revert;
var textbox = this;
var result = value;
edits[settings.name] = [value];
var returned = $.ajax({
url: "http://URLTOPOSTTO",
type: "POST",
data : edits,
dataType : "json",
complete : function (xhr, textStatus)
{
var response = $.secureEvalJSON(xhr.responseText);
if (response.Message != "")
{
alert(Message);
}
}
});
return(result);
}
Yes, there is a "onreset" parameter that is called when clicking cancel, or more generally, before jEditable resets the control back to the state before it was clicked. Add this to your code:
$("#editable_text").editable(submitEdit, {
//...
onreset: jeditableReset,
//...
});
function jeditableReset(settings, original) {
// whatever you need to do here
}
This is documented in the header of the jquery.jeditable.js file.
Another note - if you don't submit on blur (you don't appear to be in the sample), the onreset event will fire then too.