Ionic Push Notification on database Insertion - node.js

Deal All,
I am working on Ionic2 which receives push notifications. It is working fine. I want to fo a step further. I need to automate the push notifications. I am using PHP with mySQL. I also have NodeJS WebApi. Is there any possibility in NodeJS or PHP to send Ionic Push notification? I have followed the following tutorial in NodeJS but I get 401 Unauthorized error:
ionic-push-server
Following is the error:
STATUS: 401
HEADERS: {"date":"Thu, 13 Apr 2017 19:13:09 GMT","content-type":"application/json; charset=utf-8","content-length":"196","connection":"close","server":"openresty","via":"1.1 vegur","access-control-allow-credentials":"true","access-control-allow-methods":"HEAD,GET,POST,PATCH,PUT,DELETE,OPTIONS","access-control-allow-headers":"DNT,Authorization,X-CSRFToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type"}
BODY: {"meta": {"version": "2.0.0-beta.0", "status": 401, "request_id": "00000000-1111-2222-3333-444444444444"}, "error": {"link": null, "type": "Unauthorized", "message": "JWT decode error occurred."}}

var ionicPushServer = require('ionic-push-server');
var credentials = {
IonicApplicationID : "IonicAppId",
IonicApplicationAPItoken : "IonicApplicationAPIToken" //You need to generate from app settings
};
var notification = {
"tokens": ["your", "device", "tokens"],
"profile": "AppProfileNameInSmallLetters",
"send_to_all": true,
"notification": {
"title": "Hi",
"message": "Hello world!",
"android": {
"title": "Hey",
"message": "Hello Android!",
"payload": {
"category": "Star"
}
},
"ios": {
"title": "Howdy",
"message": "Hello iOS!",
"payload": {
"category": "Star"
}
}
}
};
ionicPushServer(credentials, notification);

Related

Azure REST API for running builds or pipelines

I am trying to automate the creation of Azure Pipelines for a particular branch using their REST api.
However, I am struggling to use almost all their API's, as their documentation lacks examples.
Things like List and Get are simple enough.
However, when it comes to queuing a build:
https://learn.microsoft.com/en-us/rest/api/azure/devops/build/builds/queue?view=azure-devops-rest-6.0
POST https://dev.azure.com/{organization}/{project}/_apis/build/builds?api-version=6.0
{
"parameters": <parameters>, // how do i send paramters
"definition": {
"id": 1
},
"sourceBranch": "refs/heads/feature/my-pipeline",
"sourceVersion": "d265f01aeb4e677a25725f44f20ceb3ff1d7d767"
}
I am currently struggling to send parameters.
I have tried:
Simple JSON like:
"parameters": {
"appId": "bab",
"platform": "android",
"isDemo": true
}
and stringify version of JSON like:
"parameters": "{\"appId\": \"bab\",\"platform\": \"android\",\"isDemo\": true}"
but none seems to work.
It keeps giving me the error:
{
"$id": "1",
"customProperties": {
"ValidationResults": [
{
"result": "error",
"message": "A value for the 'appId' parameter must be provided."
},
{
"result": "error",
"message": "A value for the 'platform' parameter must be provided."
},
{
"result": "error",
"message": "A value for the 'isDemo' parameter must be provided."
}
]
},
"innerException": null,
"message": "Could not queue the build because there were validation errors or warnings.",
"typeName": "Microsoft.TeamFoundation.Build.WebApi.BuildRequestValidationFailedException, Microsoft.TeamFoundation.Build2.WebApi",
"typeKey": "BuildRequestValidationFailedException",
"errorCode": 0,
"eventId": 3000
}
The docs is very unclear in how to send this data: https://learn.microsoft.com/en-us/rest/api/azure/devops/build/builds/queue?view=azure-devops-rest-6.1#propertiescollection
Thank you very much for you help.
I believe you cannot pass runtime parameters trough the Queue API. Instead, use Runs API
With that, your request body (use Content-type: application/json) should look something similar to this:
{
"resources": {
"repositories": {
"self": {
"refName": "refs/heads/feature/my-pipeline"
}
}
},
"templateParameters": {
"appId": "bab"
"platform": "android"
"isDemo": true
}
}
I just realized that in the api-version=6.0 you can also send templateParameters on the Queue Service:
POST https://dev.azure.com/{organization}/{project}/_apis/build/builds?sourceBuildId={BUILD_BUILDID}&api-version=6.0
{
"templateParameters": { "doReleaseBuild": "True" },
"definition": {
"id": 1
},
"sourceBranch": "refs/heads/feature/my-pipeline",
"sourceVersion": "d265f01aeb4e677a25725f44f20ceb3ff1d7d767"
}

Loopback error without strong-error-handler

If I have strong-error-handler in my middleware.json (i.e "final": { "strong-error-handler": {} }) then the error response structure looks like this:
{
"error": {
"statusCode": 500,
"message": "Your Error Message",
"code": "123"
}
}
But when I remove strong-error-handler from middleware.json, then the error structure looks like this:
{
"errors": [
{
"status": 500,
"source": {},
"title": "Error",
"code": "123",
"detail": "Your Error Message"
}
]
}
I can't seem to figure out how/where that error body is made when I'm not using strong-error-handler. Is there a default?
Figured it out!! Turns out it was using the errorHandler from loopback-component-jsonapi node module.

DialogFlow Webhook works in DF but not Google Assistant Simulator

I've resorted to returning a default response in order to try to get this to work. This is hosted on AWS Lambda with an API Gateway. Last night I had some different code working, but now I can't seem to get anything to work through Google Assistant.
Here's the example V2 response I am using:
callback(null, {
"payload": {
"google": {
"expectUserResponse": true,
"richResponse": {
"items": [
{
"simpleResponse": {
"textToSpeech": "Choose a item"
}
}
]
},
"systemIntent": {
"intent": "actions.intent.OPTION",
"data": {
"#type": "type.googleapis.com/google.actions.v2.OptionValueSpec",
"listSelect": {
"title": "Hello",
"items": [
{
"optionInfo": {
"key": "first title key"
},
"description": "first description",
"image": {
"url": "https://developers.google.com/actions/images/badges/XPM_BADGING_GoogleAssistant_VER.png",
"accessibilityText": "first alt"
},
"title": "first title"
},
{
"optionInfo": {
"key": "second"
},
"description": "second description",
"image": {
"url": "https://lh3.googleusercontent.com/Nu3a6F80WfixUqf_ec_vgXy_c0-0r4VLJRXjVFF_X_CIilEu8B9fT35qyTEj_PEsKw",
"accessibilityText": "second alt"
},
"title": "second title"
}
]
}
}
}
}
}
});
Here's the log output from Google Assistant to DialogFlow:
Received response from agent with body: HTTP/1.1 200 OK Server: nginx/1.13.6 Date: Sat, 11 May 2019 18:44:24 GMT Content-Type: application/json;charset=UTF-8 Content-Length: 772 X-Cloud-Trace-Context: e62f0526a5238882dd1c1a3b3a70e3b5/11459115340038791606;o=0 Google-Actions-API-Version: 2 Assistant-Interaction-Error-Code: -1 Assistant-Interaction-Error-Message: Failed to parse Dialogflow response into AppResponse because of empty speech response X-SHARD: default Via: 1.1 google Alt-Svc: clear
{
"responseMetadata": {
"status": {
"code": 10,
"message": "Failed to parse Dialogflow response into AppResponse because of empty speech response",
"details": [
{
"#type": "type.googleapis.com/google.protobuf.Value",
"value": "{\"id\":\"99120b53-f0a7-415f-b44e-73c7525fcd43\",\"timestamp\":\"2019-05-11T18:44:24.422Z\",\"lang\":\"en-us\",\"result\":{},\"alternateResult\":{},\"status\":{\"code\":206,\"errorType\":\"partial_content\",\"errorDetails\":\"Webhook call failed. Error: Failed to parse webhook JSON response: Cannot find field: errorMessage in message google.cloud.dialogflow.v2.WebhookResponse.\"},\"sessionId\":\"ABwppHHok5GTHl8A6XWq9q-Yp1chkoZ4vT688i4HTxVgVQDcY1zHH2JUqVeCMPc_6bbA7WFf1e-nw0ZQxQ\"}"
}
]
}
}
}
I'm not sure where to go from here...Everything seems to be working correctly, I just can't figure out where the parser error is with Google Assistant. What gives?
Thanks.

"Signature is Invalid" message for AppCenter Webhook request

I am trying to write a proxy service between GitHub API and AppCenter API and when I am trying to reroute a standard GitHub API message I am getting a "Signature is invalid" response.
Here's what I am attempting to send:
{
"ref": "refs/heads/xxxx",
"before": "xxxxxxxxxxxxx",
"after": "xxxxxxxxxxxxx",
"created": false,
"deleted": false,
"forced": false,
"base_ref": null,
"compare": "https://github.com/xxxxx",
"commits": [{
"content": "xxxx"
}],
"head_commit": {
"content": "xxxx"
},
"repository": {
"content": "xxxx"
},
"pusher": {
"content": "xxxx"
},
"organization": {
"content": "xxxx"
},
"sender": {
"content": "xxxx"
}
}
AppCenter's endpoint is:
https://api.appcenter.ms/v0.1/public/apps/xxxxxxxxxxx/hooks
Request's headers are:
{
"content-type": "application/json",
"User-Agent": "GitHub-Hookshot/xxxxxxxx",
"X-GitHub-Delivery": "xxxxxxxxxxxxxx",
"X-GitHub-Event": "push",
"X-Hub-Signature": "sha1=xxxxxxxxxxx"
}
Here's the response I am getting:
{
"id": "xxxxxxxxxxxxxxxx",
"message": "Signature is invalid"
}
I've yet to receive a sensible answer from AppCenter, hope somebody already had a similar experience and can answer.
Thanks
I think the issue is the value of X-Hub-Signature. From Validating payloads from Github, Github uses a HMAC SHA1 between the payload data & the secret string you have put in the webhook section of your repository :
The signature format (without brackets):
sha1={HMAC-SHA1(secret, payload)}
A few examples to compute the signature :
using ruby (from here) :
signature = 'sha1=' + OpenSSL::HMAC.hexdigest(OpenSSL::Digest.new('sha1'), ENV['SECRET_TOKEN'], payload_body)
using javascript (from here) :
var signature = 'sha1=' + CryptoJS.HmacSHA1(payload, environment.secret).toString(CryptoJS.enc.Hex)

Gocardless - Redirect flow in Node JS

I'm trying to use Gocardless to enable SEPA payment in my website.
The example in the API doc doesn't work and seems uncomplete
POST https://api.gocardless.com/redirect_flows HTTP/1.1
{
"redirect_flows": {
"description": "Wine boxes",
"session_token": "SESS_wSs0uGYMISxzqOBq",
"success_redirect_url": "https://example.com/pay/confirm",
"prefilled_customer": {
"given_name": "Frank",
"family_name": "Osborne",
"email": "frank.osborne#acmeplc.com"
}
}
}
That's the response I get :
{
"error": {
"message": "not found",
"errors": [
{
"reason": "not_found",
"message": "not found"
}
],
"documentation_url": "https://developer.gocardless.com/api-reference#not_found",
"type": "invalid_api_usage",
"request_id": "7ae43821-345d-4ffd-98d6-15c4fe5513e6",
"code": 404
}
}
How can it work if the access token of the application is never asked for the request ?

Resources