How to use/consume an event stream from wolkenkit-eventstore - wolkenkit

I want to use wolkenkit's eventstore and was trying to set up a quick example. But I'm not able to simply output an event stream.
Simplified example:
const eventstore = require("wolkenkit-eventstore/inmemory");
const Stream = require("stream");
const uuidv4 = require("uuid/v4");
const Event = require("commands-events/dist/Event");
const main = async () => {
await eventstore.initialize();
const aggregateId = uuidv4();
const event = new Event({ ... });
event.metadata.revision = 1;
await eventstore.saveEvents({ events: event });
const writableStream = new Stream.Writable();
writableStream._write = (chunk, encoding, next) => {
console.log(chunk.toString());
next()
};
const readableStream = eventstore.getUnpublishedEventStream();
readableStream.pipe(writableStream);
};
main();
As far as I understand, getUnpublishedEventStream returns a readable stream. I followed this instructions, but it didn't work as expected.
All I get is the following error:
(node:10988) UnhandledPromiseRejectionWarning: TypeError: readableStream.pipe is not a function

According to the documentation of wolkenkit-eventstore, getUnpublishedEventStream is an async function, i.e. you have to call it with await. Otherwise, you don't get a stream back, but a promise (and a promise doesn't have a pipe function).
So, this line
const readableStream = eventstore.getUnpublishedEventStream();
should be:
const readableStream = await eventstore.getUnpublishedEventStream();
I have not taken a closer look at your code apart from this, but this is the reason why you get the current error message.
PS: Please note that I am one of the core developers of wolkenkit, so please take my answer with a grain of salt.

Related

How to look for the specific index of an array once i enter the mortgage id number in twilio function?

This is my code in twilio functions,
exports.handler = function (context, event, callback) {
const got = require('got');
const mortgageInfo = event.mortgageInfo;
const mortgage_number = event.mortgage_number;
got('https://cyan-sparrow-7490.twil.io/assets/data6.json').then (response =>{
const mort = JSON.parse(response.body);
let mortgageData = mort;
//Begin filter code
let val = mortgageInfo;
// I think this line here is my problem
let index = mortgageData.map(function(e) { return e.mortgageid.value; }).indexOf(val);
// I think this line here is my problem
//End filter code
let mortgageSpecificData = mortgageData[index];
callback(null,mortgageSpecificData);
});
===========SAMPLE ASSET ==============
===========SAMPLE ASSET ==============
I think it can be a lot simpler than that. One thing to note is that event.mortgageInfo is going to be a string, but in your data, the mortgageid property is a number. So when you compare, you need to convert first.
Here's a function that I believe will work:
const got = require("got");
exports.handler = function (context, event, callback) {
const mortgageInfo = parseInt(event.mortgageInfo, 10);
got("https://cyan-sparrow-7490.twil.io/assets/data6.json").then(
(response) => {
const mortgageData = JSON.parse(response.body);
const mortgageSpecificData = mortgageData.find(
(mortgage) => mortgage.mortgageid === mortgageInfo
);
callback(null, mortgageSpecificData);
}
);
};
In this case I first change the mortgageInfo into a number by calling parseInt on the event.mortgageInfo. Then, once you fetch the data and parse it, you can find the object with the matching mortgageid using the array's find method. find takes a function that is called with each member of the array and returns when it evaluates to true. In this case, for each member of the array we check whether the mortgageid matches the mortgageInfo we got earlier.
Try this:
const index = mortgageData.findIndex((e) => e.mortgagid === mortgagid)

Receiving and handling media messages in Twilio-dialogflow WhatsApp integration

I have deployed a server on GCP to receive message traffic from twilio via webhook and integrated it with Google's dialogflow. You can see the original project here "https://github.com/GoogleCloudPlatform/dialogflow-integrations#readme".
The function works fine for receiving and responding via intent detection but it can't handle any media inputs from a user as dialogflow can't interpret it. I've been trying to code a simple IF statement that converts any input media into URL's prior to processing by dialogflow. The full code server.js file is given below:
const express = require('express');
const request = require('request');
const app = express();
const dialogflowSessionClient = require('../botlib/dialogflow_session_client.js');
const bodyParser = require('body-parser');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
const projectId = 'PROJECT-ID';
const phoneNumber = "+1##########";
const accountSid = '*********************';
const authToken = '*******************';
const client = require('twilio')(accountSid, authToken);
const MessagingResponse = require('twilio').twiml.MessagingResponse;
const sessionClient = new dialogflowSessionClient(projectId);
const listener = app.listen(process.env.PORT, function() {
console.log('listner marker');
console.log('Your Twilio01 integration server is listening on port '+ listener.address().port);
});
app.post('/', async function(req, res) {
const body = req.body;
const text = body.Body;
const id = body.From;
console.log('body marker');
const dialogflowResponse = (await sessionClient.detectIntent(text, id, body)).fulfillmentText;
const twiml = new MessagingResponse();
const message = twiml.message(dialogflowResponse);
res.send(twiml.toString());});
process.on('SIGTERM', () => {
listener.close(() => {
console.log('Closing http server.');
process.exit(0);
});
});
I have tried to add my IF statement like below but it fails to execute when placed into the main file.
if (MessagingResponse.NumMedia != "0") {
console.log(MessagingResponse.MediaUrl0);
MessagingResponse = MessagingResponse.MediaUrl0;
console.log(response.toString());
}
you cannot change the const variable value on the next statement so
change your
const text;
to
var text;
and add
if(body.MediaUrl0){
text = body.MediaUrl0;
}
before
const dialogflowResponse = (await sessionClient.detectIntent(
text, id, body)).fulfillmentText;
Twilio developer evangelist here.
You're trying to read the NumMedia and other properties from the MessagingResponse class. You should be trying to read it from the body of the incoming request: req.body.NumMedia.
Edit
Checked your code from the repo. The const still doesn't seem right. Try this:
app.post("/", async function (req, res) {
const body = req.body;
let text;
if (body.NumMedia != "0") {
text = body.MediaUrl0;
} else {
text = body.Body;
}
const id = body.From;
const dialogflowResponse = (await sessionClient.detectIntent(text, id, body))
.fulfillmentText;
const twiml = new MessagingResponse();
twiml.message(dialogflowResponse);
res.send(twiml.toString());
});
In this case we define text with let instead of const and outside of the conditional. That way we can reassign to text and use it later in the function too.

How can I get a file checksum in Deno?

Just starting with Deno, I am trying to figure out how to calculate a binary file checksum. It seems to me that the problem is not with the methods provided by the hash module of the standard library, but with the file streaming method and/or the type of the chunks feeding the hash.update method.
I have been trying a few alternatives, related to file opening and chunk types,with no success. A simple example is in the following:
import {createHash} from "https://deno.land/std#0.80.0/hash/mod.ts";
const file= new File(["my_big_folder.tar.gz"], "./my_big_folder.tar.gz");
const iterator = file.stream() .getIterator();
const hash = createHash("md5");
for await( let chunk of iterator){
hash.update(chunk);
}
console.log(hash.toString()); //b35edd0be7acc21cae8490a17c545928
This code compiles and runs with no errors, pity that the result is different from what I get running the functions of the crypto module provided by node and the md5sum provided by linux coreutils. Any suggestion ?
nodejs code:
const crypto = require('crypto');
const fs = require('fs');
const hash = crypto.createHash('md5');
const file = './my_big_folder.tar.gz';
const stream = fs.ReadStream(file);
stream.on('data', data=> { hash.update(data); });
stream.on('end', ()=> {
console.log(hash.digest('hex')); //c18f5eac67656328f7c4ec5d0ef5b96f
});
The same result in bash:
$ md5sum ./my_big_folder.tar.gz
$ c18f5eac67656328f7c4ec5d0ef5b96f ./my_big_folder.tar.gz
on Windows 10 this can be used:
CertUtil -hashfile ./my_big_folder.tar.gz md5
The File API isn't used to read a File in Deno, to do that you need to use the Deno.open API and then turn it into an iterable like this
import {createHash} from "https://deno.land/std#0.80.0/hash/mod.ts";
const hash = createHash("md5");
const file = await Deno.open(new URL(
"./BigFile.tar.gz",
import.meta.url, //This is needed cause JavaScript paths are relative to main script not current file
));
for await (const chunk of Deno.iter(file)) {
hash.update(chunk);
}
console.log(hash.toString());
Deno.close(file.rid);
import { crypto, toHashString } from 'https://deno.land/std#0.176.0/crypto/mod.ts';
const getFileBuffer = (filePath: string) => {
const file = Deno.openSync(filePath);
const buf = new Uint8Array(file.statSync().size);
file.readSync(buf);
file.close();
return buf;
};
const getMd5OfBuffer = (data: BufferSource) => toHashString(crypto.subtle.digestSync('MD5', data));
export const getFileMd5 = (filePath: string) => getMd5OfBuffer(getFileBuffer(filePath));

how can i save received image or document file from sender whatsapp using twilio?

I have a scenario like user will send the image or pdf file to twilio whatsapp number so i need to save that image/pdf in folder which will be processed to next level.
How can i save the files? I am using Node SDK.
Thanks in advance.
Assuming you've already configured your webhook on your sandbox page, so that messages containing media from Whatsapp are getting to your app.
As documentation says, you'll receive MediaContentType{N} and MediaUrl{N} as long as body and other parameters. The following snippet was translated-ish from a Python example from official documentation:
const Fs = require('fs')
const Path = require('path')
const Axios = require('axios')
const num_media = req.body.NumMedia;
const media_files = []
for (let i = 0; i <= num_media; i++) {
const id = req.body.MessageSid
const media_url = req.body[`MediaUrl{i}`];
const mime_type = req.body[`MediaContentType{i}`);
media_files.push({'media_url': media_url, 'mime_type': mime_type});
download(media_url, id);
}
async function download(url, name) {
const path = Path.resolve(__dirname, 'files', name)
const writer = Fs.createWriteStream(path)
const response = await Axios({
url,
method: 'GET',
responseType: 'stream'
})
response.data.pipe(writer)
return new Promise((resolve, reject) => {
writer.on('finish', resolve)
writer.on('error', reject)
})
}

Twilio Studio Not Listing Services

I am setting up a Sync Application using Twilio's Sync Library. For some reason, none of the REST API methods seem to work. That is, I cannot get any of the sync methods to console.log() anything via the runtime functions.
I can, however, console.log() plain text.
Here is my code:
exports.handler = function(context, event, callback) {
// 0. Init
// const phoneNumber = event.phoneNumber;
const issueLimit = 3; // CHANGE IF NEEDED
const listName = 'issues';
const twilioClient = context.getTwilioClient();
// 1. List all lists
twilioClient.sync.services(context.SYNC_SERVICE_SID)
.syncLists
.list({limit: 20})
.then(syncLists => syncLists.forEach(s => console.log(s.sid)));
// 2. return true if quota reached
console.log("Got to here");
// 3. return false
callback(null, undefined);
};
The only code that appears to execute is the 'console.log("Got to here");'. I'm also not receiving any error messages.
Any guidance is sincerely appreciated.
When you see .then(), that's a promise, and you can read more about this here https://www.twilio.com/blog/2016/10/guide-to-javascript-promises.html
In other words, the JavaScript engine goes to your steps 2. and then 3. without waiting for 1. to finish. And since you're returning at step 3 with callback(null, undefined); you won't see the logs.
So, you'll have to move callback() inside the .then(), something like this:
exports.handler = function (context, event, callback) {
// 0. Init
// const phoneNumber = event.phoneNumber;
const issueLimit = 3; // CHANGE IF NEEDED
const listName = 'issues';
const twilioClient = context.getTwilioClient();
// 1. List all lists
twilioClient.sync.services(context.SYNC_SERVICE_SID)
.syncLists
.list({ limit: 20 })
.then(
function (syncLists) {
console.log("Got to here");
syncLists.forEach(s => console.log(s.sid));
callback(null, undefined);
}
);
};

Resources