Call a Javascript function in Android using Jasonette - javascript

I´m turning my website into an app using Jasonette, but I'm having trouble passing java values to javascript after success.
I'm using the following code that is working fine:
JSON:
"actions": {
"$foreground": {
"type": "$reload"
},
"test_action": {
"type": "$util.banner2",
"success": {
"type": "$agent.request",
"options": {
"id": "$webcontainer",
"method": "testEcho"
}
}
}
Javascript:
$agent.trigger("test_action");
function testEcho(){
$('#wrapper').find('#test').css({'color':'red'});
}
Java:
JSONObject json_test_value=new JSONObject();
String test_value = "xpto";
json_test_value.put("value1", test_value);
JasonHelper.next("success", action, json_test_value, event, context);
I want to get the test_value from java after success, in order to be able to use testEcho (test_value).
It seems simple but for some reason I can not do.

Related

Getting parameters from an Azure Function to use in function.json bindings

I am new to Azure Functions and I'm having trouble with some of the basics, in particular how to pass parameter data to function.json so that I can write a blob to Azure Blob Storage using the Storage Connector.
My question is, how do I specify a parameter within the httpTrigger function that can be used by the outputBlobContents binding below?
My setup is pretty simple (but doesn't work yet):
function.json:
{
"bindings": [
{
"authLevel": "function",
"type": "httpTrigger",
"direction": "in",
"name": "req",
"methods": [
"post"
]
},
{
"type": "http",
"direction": "out",
"name": "res"
},
{
"type": "blob",
"direction": "out",
"name": "outputBlobContents",
"path": "uploaded_files/{destinationFilename}",
"connection": "MY_STORAGE"
}
]
}
index.ts:
import { AzureFunction, Context, HttpRequest } from "#azure/functions"
const httpTrigger: AzureFunction = async function (context: Context, req: HttpRequest): Promise<void> {
context.bindingData.destinationFilename = "testfile.pdf";
context.bindings.outputBlobContents = context.req.body;
const responseMessage = "uploaded file";
context.res = {
status: 201,
body: responseMessage
};
};
export default httpTrigger;
In my example code I am trying assign destinationFilename to the context.bindingData object, but this does not work. I've read through all the documentation but it's not very clear about what the bindingData object actually is or how named parameters work in general. How do I tell the blob storage connector where to store the file?
Simply put, any form of json input will be obtained as a parameter. Inside the function is impossible. If you want to use it inside the function, please choose to directly use the sdk of Azure Storage.
Since you use httptrigger, you can send a request body in json format, like this:
{
"destinationFilename":"something"
}
After that, output binding can get the value.

How to get JSON payload sent by the Slack action message?

I am trying to build a Slack bot with interactive buttons. I have set up a Google Apps Script to handle the action performed on the Slack message. I want the payload of the request sent by the Slack. I have tried to get the request object by doing
function doPost(e) {
return processComment(e);
}
function processComment(e) {
Logger.log(e);
}
{postData=FileUpload, queryString=method=slack, parameter={method=slack, payload={"type":"block_actions","user":{"id":"U01835Mxxxx","username":"ravsamteam","name":"ravsamteam","team_id":"T0160UQZZZZ"},"api_app_id":"A018MPZ2xxx","token":"NTNRCTPDz8mxxxzxxxxxxxxx","container":{"channel_id":"C0190D8L2AU","is_ephemeral":false,"message_ts":"1597154895.001500","type":"message"},"trigger_id":"1281039280903.1204976558018.aa1055f6900d7884d9cd4ac34ffzzzzz","team":{"id":"T0160UQGE0J","domain":"ravsamhq"},"channel":{"id":"C0190D8L2AU","name":"blogs"},"message":{"type":"message","subtype":"bot_message","text":"This content can't be displayed.","ts":"1597154895.001500","bot_id":"B019BNL08BS","blocks":[{"type":"section","block_id":"mNavk","text":{"type":"mrkdwn","text":" New comment on RavSam's blog by hello","verbatim":false}},{"type":"section","block_id":"v3Ip","text":{"type":"mrkdwn","text":"*Blog:*\nhello\n\n*Comment:*\nravgeet errorCannot read property 'payload' of undefined","verbatim":false}},{"type":"actions","block_id":"1maVO","elements":[{"type":"button","action_id":"WSo=","text":{"type":"plain_text","text":"Approve","emoji":true},"style":"primary","value":"approved"},{"type":"button","action_id":"Vek\/","text":{"type":"plain_text","text":"Deny","emoji":true},"style":"danger","value":"denied"}]}]},"response_url":"https:\/\/hooks.slack.com\/actions\/T0160Uxxxxx\/1301968xxxxxx\/Q3gZhbeUCUIxxxxxxxxxxxxx","actions":[{"action_id":"WSo=","block_id":"1maVO","text":{"type":"plain_text","text":"Approve","emoji":true},"type":"button","value":"approved","action_ts":"1597213837.152704"}]}}, contentLength=2391.0, parameters={payload=[Ljava.lang.Object;#53f2e9fa, method=[Ljava.lang.Object;#5793298b}, contextPath=}
How do I get the payload? Once I have the payload JSON, I can use the actions to determine what action was taken by the user?
Yes. The payload contains all the information you need to identify the action. And it also contains a response_url to respond back.
Slack payload should look like this.
{
"actions": [
{
"name": "channels_list",
"selected_options": [
{
"value": "C012AB3CD"
}
]
}
],
"callback_id": "select_simple_1234",
"team": {
"id": "T012AB0A1",
"domain": "pocket-calculator"
},
"channel": {
"id": "C012AB3CD",
"name": "general"
},
"user": {
"id": "U012A1BCD",
"name": "musik"
},
"action_ts": "1481579588.685999",
"message_ts": "1481579582.000003",
"attachment_id": "1",
"token": "iUeRJkkRC9RMMvSRTd8gdq2m",
"response_url": "https://hooks.slack.com/actions/T012AB0A1/123456789/JpmK0yzoZDeRiqfeduTBYXWQ",
"trigger_id": "13345224609.738474920.8088930838d88f008e0"
}
You can learn more here.

Messenger sharing causes "Messenger Extensions unexpected error"

I'm trying to perform a share with the beginShareFlow()function of the facebook MessengerExtensions. However, when performing the call, I get following error message:
Messenger Extensions unexpected error.
The code for this error is 2018154.
I'm using following code to start a share:
ajax.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
console.log(this.responseText);
var message = JSON.parse(this.responseText);
MessengerExtensions.beginShareFlow(function(share_response) {
if(share_response.is_sent){
// close the window w/ requestCloseBrowser().
}
},
function(errorCode, errorMessage) {
console.log(errorCode+"-"+errorMessage);
},
message,
"current_thread");
}
}
You'll see that I'm using the response of an ajax call to fill up the actual message. This is generated by some backend php code, an example:
{"attachment":{"type":"template","payload":{"template_type":"generic","sharable":"true","elements":[{"title":"Kerk Poederlee","image_url":"https:\/\/www.wouterh.be\/pogo\/static\/images\/raid.jpg","subtitle":"joining at 1535629500","default_action":{"type":"web_url","url":"https:\/\/www.wouterh.be\/pogo"},"buttons":[{"type":"web_url","url":"https:\/\/www.wouterh.be\/pogo","title":"join raid"}]}]}}}
Messenger extension themselves have been loaded correctly, since I'm able to retrieve the PSID with following code:
window.extAsyncInit = function() {
MessengerExtensions.getContext('APPID',
function success(thread_context){
fb_user_id = thread_context.psid;
},
function error(err){}
);
};
This error occurs on a web client and on the most recent iOS messenger. When I send the exact same message through the Send API instead of the sharing messenger extensions, it works perfectly. Could somebody help me figure out what's causing the error?
I found the mistake, in the message you can pass a 'sharable' property. I passed the string value "true" to it, but this should be a boolean value.
This is the corrected content:
{
"attachment": {
"type": "template",
"payload": {
"template_type": "generic",
"sharable": true,
"elements": [{
"title": "Kerk Poederlee",
"image_url": "https:\/\/www.wouterh.be\/pogo\/static\/images\/raid.jpg",
"subtitle": "joining at 1535629500",
"default_action": {
"type": "web_url",
"url": "https:\/\/www.wouterh.be\/pogo"
},
"buttons": [{
"type": "web_url",
"url": "https:\/\/www.wouterh.be\/pogo",
"title": "join raid"
}]
}]
}
}
}

converting javascript program from json to xml

I am rewriting a javascript program that uses json files to one that uses xml files for the settings. The xml files that I have do not have the same structure as the json files and some of the json variables I intend to hard code into the program. I am thinking it would be easiest not recode all of the calls to the json data, so I am trying to create objects that look like the json data and have my own functions to load the xml data into the objects where needed.
Note I have never messed with json before. if you think there may be a better approach I would be happy to try it.
Simple json files I have figured out, just make a class that looks the same.
{
"logo": "yes",
"title": "Jordan Pond",
"author": "Matthew Petroff",
"license": 1,
"preview": "../examples/examplepano-preview.jpg",
}
turns into
function config(){
this.logo='yes';
this.title='Jordan Pond';
this.author='Matthew Petroff';
this.license='1';
this.preview='./examples/examplepano-preview.jpg';
}
config= new config();
alert(config.title);
Do that and all of the calls to the json stuff works the same as always. I am stumped on the more complicated json files such as
{
"default": {
"license": 0,
"logo": "no",
"author": "Kvtours.com",
"firstScene": "WilsonRiverFishingHole",
"title": "Wilson"
},
"scenes": {
"pond": {
"title": "Jordan Pond",
"preview": "../examples/examplepano-preview.jpg",
"panorama": "../examples/examplepano.jpg"
}
}
}
I would think that should convert to something like this.
function tourConfig(){
this.default= function() {
license= "0";
logo= "no";
author= "Kvtours.com";
firstScene= "pondCube";
title= "Wilson";
}
this.scenes= function(){
this.pondCube= function() {
this.title= "Jordan Pond (Cube)";
this.preview="examples/examplepano-preview.jpg";
this.panorama="../examples/examplepano.jpg";
}
}
}
tourConfig= new tourConfig();
alert(tourConfig.default.author);
That is not working. Any thoughts on how to get it to work?
You are confused on javascript closure.
I think what you want is as follow:
function tourConfig(){
var that = this;
this.default= function() {
that.license= "0";
that.logo= "no";
that.author= "Kvtours.com";
that.firstScene= "pondCube";
that.title= "Wilson";
}
// as well as scenes method
}

Iteration in handlebar using backbone

I'm using backbone and handlebars for templating and i'm new to this.
My current json is in the below format and the code works fine.
[
{
"id": "10",
"info": {
"name": "data10"
}
},
{
"id": "11",
"info": {
"name": "data11"
}
}
]
But when i change my json structure to something like shown below i'm having difficulty in getting things to be populated.
{
"total_count": "10",
"dataElements": [
{
"id": "10",
"info": {
"name": "data10"
}
},
{
"id": "11",
"info": {
"name": "data11"
}
}
]
}
How can i populate name, info and total_count keeping the current code structure ?
JSFiddle : http://jsfiddle.net/KTj2K/1/
Any help really appriciated.
A few things that you need to do in order for this to work.
Replace Backbone's core 'reset' on your collection with a custom one that understands the data you are passing to it. For example:
reset: function (data) {
this.totalCount = data.total_count;
Backbone.Collection.prototype.reset.call(this, data.dataElements);
}
Now when you reset your collection, it will pull the total_count out of the object you are resetting it with, and use Backbone's core reset with the dataElement array. Keep in mind you may have to do a similar thing with 'parse' if you're intending on pulling this from the server.
I'd recommend that (if your example looks anything like the real code you're working with) you reset your collection before getting to rendering.
var dataCollectionList = new dataCollection();
dataCollectionList.reset(jsonData);
var App = new AppView({model : dataCollectionList});
Now in your view's "render" method you can grab the 'totalCount' property off the collection -
render : function() {
//Should spit the total count into the element, just as an example
this.$el.append(this.model.totalCount);
//or console.log it
console.log(this.model.totalCount);
return this;
}
Voila. Side note - as someone who works with Backbone a lot, it drives me nuts when people set an attribute of something like "model" (i.e. peopleModel, itemModel, etc) and it ends up being a backbone collection. It's much clearer to name it after what it is - though some MVC purists may disagree a bit.
Also, in this code block:
_.each(this.model.models, function (myData) {
$(this.el).append(new ItemView({model:myData}).render().el);
}, this);
You don't need to do _.each(this.model.models.......). Since you're working with a collection, the collection has a built in 'each' method.
this.model.each(function (myData) { ..... } , this);
Quite a bit cleaner.

Categories

Resources