Calling Watson Conversation API from behind a Proxy server using Node.js - node.js

I am using Watson Node.js SDK https://www.npmjs.com/package/watson-developer-cloud to send a message to Watson Conversation service, but I'm behind a http proxy and not able to reach to the Watson service. How might we configure proxy details (either inside or outside of Node.js) so that the the outbound API call can be made?
var Watson = require( 'watson-developer-cloud/conversation/v1' ); // watson sdk
// Create the service wrapper
var conversation = new Watson( {
username: '<username>',
password: '<password>',
url: 'https://gateway.watsonplatform.net/conversation/api',
version_date: '2016-09-20',
version: 'v1'
});
conversation.message( payload, function(err, data) {
...
});

Not completely sure about this but can you set the environment variable for http_proxy? I believe that this should force the use of the proxy regardless of the what the developer cloud module does.
process.env.http_proxy = "https://YOUR_PROXY_HOST:YOUR_PROXY_PORT";
If that works you should set the variable using:
npm set <key> <value>

Related

shopify app development changing graphql API version

I am trying to change the version of the graphql API the app is using.
setup:
shopify CLI ver 2.15.6
type node.js
in code I cannot see the URL the app is calling, is there an ENV or some server side
settings to change to the latest API version?
on next.js I see this on the server/client.js
export const createClient = (shop, accessToken) => {
return new ApolloClient({
uri: `https://${shop}/admin/api/2019-10/graphql.json`,
but this is not used in embedded app bridge calls.
I would like to use the 2022-04 (latest) version
I think you need to initialise it using the context, according to the docs
While setting up Context, you'll be able to set which version of the
Admin API your app will be using.
Shopify.Context.initialize({
API_KEY,
API_SECRET_KEY,
SCOPES: [SCOPES],
HOST_NAME: HOST.replace(/https:\/\//, ""),
IS_EMBEDDED_APP: {boolean},
API_VERSION: ApiVersion.{version} // all supported versions are available, as well as "unstable" and "unversioned"
});

How to make a post using WATSON and Node js?

I want to make a POST (add a new task) through chatting with Watson.
I have the POST function which works very well, I tested it on Postman. And I have the Watson nodes created.
Here is my endpoint from the POST in Node js:
MainRouter.post('/welcome', (req, res) => {
TaskPost.postTask(req.body).then(message => {
return res.json(message);
}).catch((error) => {
return res.status(404).json(error);
});
});
Here is my conversation with Watson (is working very well):
I also included this in the main.js and changed the password and url:
const AssistantV2 = require('ibm-watson/assistant/v2');
const { IamAuthenticator } = require('ibm-watson/auth');
const assistant = new AssistantV2({
authenticator: new IamAuthenticator({ apikey: '<apikey>' }),
serviceUrl: 'https://api.us-south.assistant.watson.cloud.ibm.com',
version: '2018-09-19'
});
How do I make this connection ? What do I have to include?
I would recommend the official Node.js SDK for the IBM Watson services. It includes support for Watson Assistant and is easy to use.
If you still want to use the V2 API directly, then take a look at how the SDK utilizes the API. As alternative, the API docs for Watson Assistant have code snippets for Node.js.
The basic flow is that you authenticate, then establish a session, and finally send messages with the user input to Watson Assistant.
To get to your dialog node with the task, the user (or your code) would need to send the right intents and entities for navigating from the root node to that specific subnode.

React native and IBM Watson

I have been using expo to build a react native app and would like to integrate an IBM Watson chatbot onto my platform. When I import the module however I receive a lot of error messages as core node modules such as os and fs seem to be missing, but aren't downloaded with node.js for some reason. When I try and add these manually, the HTTPS module is missing the index.js file. Is there any way for me to find this file or resolve this problem another way?
It's not completely clear from your question but I shall assume that you are using the Node SDK for Watson Assistant. This is designed to be run in a Node.js environment which a react native JavaScript bundle is not (with or without expo). That's why you are missing key libraries like os and fs which the Node SDK expects. Installing fs won't resolve your problem because it also expects a Node.js environment to work, hence why there are react native specific fs libraries that are able to use ios and android code to interact with the file system of the phone.
What you should be attempting is running the Node SDK on an independent server and running simple api requests using libraries like axios or for more robust production systems graphql so that your architecture will approximate this high level design.
a high level architecture diagram which shows a phone connected by an arrow reading axios api request to another box labelled cloud hosted server. From this box another arrow labelled Node SDK is pointing to another box labelled Watson Assistant
Web applications are similarly limited. The code run on the user's browsers can't directly use the Watson Assistant SDK, these requests to Watson Assistant need to be run by a server. There is an example starter Watson Assistant web application that does this. If you run or host this application you can use the same server for your requests (although bear in mind this simple app and shared traffic probably isn't scalable for anything but a proof of concept).
So rather than running the api requests to Watson Assistant directly you run them to this domain of the server and then the necessary endpoint. The server in the example app is set up to accept requests to start the session at <your domain>/api/session and to send messages at <your domain>/api/message
You optionally can run direct api calls to Watson Assistant from a react native app without the SDK. It's not advisable because you would need to store your private keys on the device where they could be viewed by anyone.
Here is a functional component that is able to complete api calls direct to Watson Assistant using the v1 of the message tool without the SDK. BUT IT IS NOT ADVISED BECAUSE I MUST STORE MY KEYS INSECURELY.
import React, { useState } from 'react';
import { View, Text, TouchableOpacity } from 'react-native';
import axios from 'axios';
import base64 from 'react-native-base64';
const workspace = ''; //replace with your own workspace id
const key = ''; //replace with your own key
const encodedKey = base64.encode(`apikey:${key}`);
// the following url will be different depending on where you host your Watson Assistant
// this is for Frankfurt as an example hence eu-de in the domain name
const assistantInstance =
'https://api.eu-de.assistant.watson.cloud.ibm.com/instances/<This is your own>/v1'; //replace with your own
const ExampleComponent = () => {
const [response, setResponse] = useState('');
const sendMessage = () => {
axios
.post(
`${assistantInstance}/workspaces/${workspace}/message?version=2018-09-20`,
{
input: { text: 'This is the message' },
},
{
headers: {
Authorization: `Basic ${encodedKey}`,
'Content-Type': 'application/json',
},
},
)
.then((data: any) => {
console.log(data);
setResponse('Got response');
})
.catch((err: any) => {
console.log(err);
setResponse('Got an error');
});
};
return (
<View>
<Text>{response}</Text>
<TouchableOpacity
onPress={() => {
sendMessage();
}}
>
Send message
</TouchableOpacity>
</View>
);
};
export default ExampleComponent;
The next complication you will find is that you will need to add code to store the context returned in the response otherwise your state is lost. Your body would end up looking something like
{
input: { text: 'This is the message' },
context: savedContextObject
},
The newer version of the API has a stateful version which you may want to use instead. You can use this axios as a pattern for constructing whatever requests your prefer.
For the third and final time PLEASE DO NOT SAVE TO THE JS FILE YOUR API KEY as I do here. This is just as an example and for proof of concepts. Anyone who downloads your app will be able to unzip your apk and read these strings in your generated JS bundle unencrypted!

How to use DocumentDB behind a Proxy in NodeJS

In NodeJS I`m trying to connect to a Cosmos DB using the Documentdb library, as the getting starter of the azure documentation says in the TODO List Example. Tutorial Here
If I use an Internet that is not behind a proxy it works.
This is the connection code:
var DocumentDBClient = require('documentdb').DocumentClient;
var docDbClient = new DocumentDBClient(config.host, {
masterKey: config.authKey
});
But when I'm behind a proxy, the connection never occur's. I`m getting an "Error: connect ETIMEDOUT"
In other node JS apps, if I want to make a request of a webservice, I just configure the proxy for the request. For example with request:
request = require('request').defaults({
proxy:'http://USERNAME:PASSWORD#proxy.url.com:8080',
});
Is there a way to configure the proxy in DocumentDB to connect to the DB in Azure (NodeJS)?
I've not personally tried it but I was going through the source code of the SDK and found out that ConnectionPolicy has a parameter called ProxyUrl. Can you try something like the following:
var connectionPolicy = new DocumentBase.ConnectionPolicy();
connectionPolicy.ProxyUrl = 'http://USERNAME:PASSWORD#proxy.url.com:8080';
var docDbClient = new DocumentDBClient(config.host, {
masterKey: config.authKey
}, connectionPolicy);

How can I use the Windows version of NGROK from behind a corporate proxy?

How can I use the Windows version of NGROK from behind a corporate proxy?
I am using Node JS and I need to reach out api.twilio.com.
Example:
// Twilio Credentials
var accountSid = 'ACCOUNTSID';
var authToken = 'AUTHTOKEN';
//require the Twilio module and create a REST client
var client = require('twilio')(accountSid, authToken);
client.messages.create({
to: "+16518675309",
from: "+14158141829",
body: "Thanks for the help StackOverflow!",
mediaUrl: "http://farm2.static.flickr.com/1075/1404618563_3ed9a44a3a.jpg",
}, function(err, message) {
console.log(message.sid);
});
Have you checked out the ngrok FAQ? The first item may answer your question:
Does ngrok work behind an HTTP Proxy?
Yes. You may specify an HTTP proxy to connect through in ngrok's configuration file or by using the standard unix environment variable http_proxy. Consult the documentation on running ngrok through an HTTP proxy for more details.

Resources