Skip to content

CrowdArt/Chat-Implementation

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Chat-Implementation

Table of Contents

APIs

Asynchronous JavaScript

Waiting for actions to finish is implicit in the synchronous model, while it is explicit, under our control, in the asynchronous one.

  • var myArray = new Array();
  • document.getElementById("id").onClick - onClick property returns the click event handler code on the current element
var myArray = new Array()=function() {
  document.getElementById('first dev').innerHTML = myArray[1];
}
  • Eloquent Javascript
  • Writing neat asynchronous Node JS code with Promises & Async/Await
  • Async/await
  • .then method - registers a callback function to be called when the promise resolves and produces a value. You can add multiple callbacks to a single promise, and they will be called, even if you add them after the promise has already resolved (finished). .then method returns another promise, which resolves to the value that the handler function returns or, if that returns a promise, waits for the promise and then resolves its result.
  • To create a promise, you can use promise as a constructor.
  • Construtor - method for creating and initializing an object created within a class.
  • Promise - based interface for the readStorage function:
function storage(nest, name) {
  return new Promise(resolve => {
    nest.readStorage(name, result => resolve(result));
  });
}

storage(bigOak, "enemies")
  .then(value => console.log('Got', value));

Main advantage of Promises is that they simplify use of callback functions.

Instead of having to pass around callbacks, promise-based functions look similar to regular ones: they take input as arguments and return their outputs.

Async code is used to load data into tables, make requests to the server, load DOM elements on priority, write non-blocking code on Node.

Creating a Promise

We create a promise in our Node.js program using the 'new' constructor.

  • var myPromise = new Promise (function(resolve, reject)){}
  • let - allows you to declare variables that are limited in scope to the block, statement, or expression on which it is used. let bindings are created at the top of the (block) scope containing the declaration => this is hoisting.
  • var keyword - defines a variable globally, or locally to entire function regardless of block scope.

There are three patterns to deal with asynchronous code:

  • Callbacks - a function to call when a result of an asynchronous operation is ready. Callbacks are used to signal completion of a task.
  • Promises - object that can hold results of an asynchronous operation. It can be in 3 states: Pending, Fulfilled and Rejected. Initially, it is an a pending state. If the operation is completed successfully it is resolved or fulfilled. If the state of the promise fails, it will go from pending to rejected. Every promise object has two method. If you have an async function that returns a callback, modify it to return a promise. A promise is an asynchronous action that may complete at some point and produce a value. It is able to notify anyone who is interested when its value is available. To get the result of a promise, you can use its then method. This registers a callback function to be called when the promise resolves and produces a value. You can add multiple callbacks to a single promise, and they will be called, even if you add them after the promise has already resolved (finished). The main advantage of promises—they simplify the use of asynchronous functions.
  • Async/await

Authentication and Authorization

  1. Register: POST /api/users {name, email, password}
email: {
  type: String,
  unique: true
}
  1. Login: POST /api/logins

Callbacks

  • Callbacks are used to signal completion of a task.
  • setTimeout function waits a given number of milliseconds and then calls a function"
setTimeout(() => console.log('Vuk'), 2000);
import {bigOak} from "./crow-tech"

bigOak.readStorage("food cache", cache = > {
   let firstCache = cache[0];
   bigOak.readStorage(firstCache, info => {
      console.log(info);
   });
});
  • .then - method registers a callback function to be called when the promise resolves and produces a value. You can add multiple callbacks to a single promise, and they will be called, even if you add them after the promise has already resolved (finished).
  • .then - method returns another promise, which resolves to value that the handler function returns or, if that returns a promise, waits for that promise then resolves its result.

Promises

It can be in 3 states: Pending, Fulfilled and Rejected. Initially, it is an a pending state. If the operation is completed successfully it is resolved or fulfilled. If the state of the promise fails, it will go from pending to rejected. Every promise object has two method.

To create a promise, you can use Promise as a constructor.

constructor - method for creating and initializing an object created within a class.

If you have an async function that returns a callback, modify it to return a promise.

A promise is an asynchronous action that may complete at some point and produce a value. It is able to notify anyone who is interested when its value is available. To get the result of a promise, you can use its then method. This registers a callback function to be called when the promise resolves and produces a value. You can add multiple callbacks to a single promise, and they will be called, even if you add them after the promise has already resolved (finished). The main advantage of promises—they simplify the use of asynchronous functions.

The easiest way to create a promise is by calling Promise.resolve. To get the result of a promise, you can use its then method.

let fifteen = Promise.resolve(15);
fifteen.then(value => console.log( 'Got ${value} ')):

Argument vs Parameter

  • Parameter is a variable in a method definition. When a method is called, the arguments are the data you pass into the method's parameters.
  • Parameter is a variable in the declaration of a function. Argument is the actual value of this variable that gets passed to function.
  • A parameter is a variable defined by a method that receives a value when the method is called.
  • An argument is a value that is passed to a method when it is invoked.
  • parameters are the name within the function definition.
  • Arguments are the values passed in when the function is called.
  • Parameters don’t change when the program is running
  • Arguments are probably going to be different every time the function is called.

Banking APIs

Chatbot Resources

Chatbot Technology Stack

  • Dialogflow
    • Dialogflow is a natural language processor used to understand what our visitors want from the bot. Components to use: agents, intents, parameters, prebuilt agents, slotfilling, etc.
  • React
  • NodeJS
  • mongoDB
  • ExpressJS
  • git
  • Heroku
  • Nodemon - local development server

  • rich messages like cards and quick replies.
  • setup React, configure local servers, create a proxy for communicating with backend.
  • Slotfilling - the task is to complete all known information about a given query entity.
  • Entity Linking - Spanning multiple documents and linking mentions to concrete entities (when possible), rather than simply clustering them.

Dialogflow

ES6

use strict; Defines that JavaScript code should be executed in "strict mode", a literal expression. With strict mode, you can not, for example, use undeclared variables.

  • With object destructuring, when declaring a variable or constant use {} and add the property of the target object.
  • Node.js - operates on a single thread event loop, using non-blocking I/O calls, allowing it to support tens of thousands of concurrent connections without incurring the cost of thread context switching.

Express

Bulding webservices on top of Node.js.

  • export PORT=5000
  • http://localhost:5000/api/post/2018/1?sortby=name
  • app.use(express.json) - when we call express.json method, the method returns a piece of middleware and then we call the app.use to use the middleware in the request processing timeline.
  • When the server creates a new object and/or resource it should write that in the body of the response.

Facebook

Facebook Messenger

Webhook

Facebook will make a request to that URL from their servers to deliver the updates – so of course it has to be publicly reachable over the internet, which a localhost address obviously isn’t.

Facebook will send a request to that URL if any data for the object and fields that you subscribed for changes. And what the data structure looks like, is described in the docs. For page fields it returns the new content directly; for user fields it will only tell you which fields have changed, so that you can then make a request for that data.

You can only get real-time updates for pages that you have admin access to. And the Public Feed API is not deprecated; but access to it is limited to a small set of Facebook partners. You can not apply to become one – if you absolutely need this kind of data, then you’ll have to contact one of those partners and have them develop a solution for you.

Quick Start for Website

Transitioning from App-level to Page-level Subscription Messaging

Graph API

Git

Google

Google Cloud SDK

Commands

  • ./google-cloud-sdk/bin/gcloud init
  • gcloud init --skip-diagnostics

Activate service account

  • ./google-cloud-sdk/bin/gcloud auth activate-service-account
  • ./google-cloud-sdk/bin/gcloud auth print-access-token

Set a variable

  • export REACT_APP_DIALOGFLOW_CLIENT_KEY=
  • export REACT_APP_DIALOGFLOW_CLIENT_ID=
  • export REACT_APP_DF_SESSION_ID=

Print variable set

  • echo $REACT_APP_DIALOGFLOW_CLIENT_KEY

google-oauth-jwt

Heroku

joi

joi allows you to create blueprints or schemas for JavaScript objects (an object that stores information) to ensure validation of key information.

Links

Chat Apps

mongoDB

CLI

  • sudo mkdir -p /data/db
  • sudo chown -R id -un /data/db
  • mongod - mongo daemon (computer program that runs as a background process)

Implementation

  • mongoose.connect('mongodb://localhost/databaseName'); //connection string for local server - it prod it should come from the config file
  • mongoose.connect(config.mongoURI, { useNewUrlParser: true });
  • connect method returns a promise

ngrok

ngrok allows you to expose a web server running on your local machine to the internet.

Steps:

  1. sudo cp ngrok /usr/local/bin
  2. /usr/local/bin/ngrok authtoken tokenxxxxxx
  3. /usr/local/bin/ngrok http 5000
  4. /usr/local/bin/ngrok http -subdomain=xxxx 5000

Node.js

NPM Packages

joi-password-complexity

  • joi-password-complexity
  • request - npm package removes the boilerplate code of inbuilt http package.
  • npm install concurrently --save-dev - Run multiple commands concurrently.

Object Oriented Programming

Four pillars of object oriented programming:

  1. Encapsulation - isolate impact of changes (reduce complexity)
  2. Abstraction - isolate impact of changes
  3. Inheritance - eliminate redundant code
  4. Polymorphism - refactor switch/case statements

PostgreSQL

React

Functional Components

  • javascript functions

Class Components

  • states
  • lifecycle hooks (componentWillMount, componentDidMount)
  • extra methods

Redux

When you have some piece of application state that needs to be mapped to multiple components

Default Components

https://reactpatterns.com/

Named Components

https://reactpatterns.com/

Unique sessions for visitors

  1. Generate unique id in React frontend
  2. Set the id to cookie
  3. Send this id to API endpoint in backend app
  4. Construct a session from global session and unique id
  5. Make a call to Dialogflow with new session

Cards

  • header
  • image
  • description
  • link
  • price

RESTful

  • callback function is: (req, res) => {}
  • use let identifier if you want to reset the variable later
  • app.use(express.json); - when we call express.json method, the method returns a piece of middleware, and then we call app.use to use the middleware in the request processing timeline.
  • When the server creates a new object and/or resource, it should write that in the body of the response

Sendgrid

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published