Skip to content

Instantly share code, notes, and snippets.

View anchetaWern's full-sized avatar
🏠
Working from home

Wern Ancheta anchetaWern

🏠
Working from home
View GitHub Profile
@anchetaWern
anchetaWern / extract Dialogflow parameters in server.js
Last active July 13, 2019 07:43
Pokedex Bot: extract Dialogflow parameters in server.js
const { intent, parameters, outputContexts, queryText } = req.body.queryResult;
const pokemon = (parameters.pokemon) ? parameters.pokemon.toLowerCase().replace('.', '-').replace(' ', '').replace("'", "") : '';
const specs = parameters.specs;
const get_type_effectiveness = (parameters.type_effectiveness) ? true : false;
let response_obj = {};
@anchetaWern
anchetaWern / add Dialogflow webhook handler in server.js
Last active July 13, 2019 07:43
Pokedex Bot: Add Dialogflow webhook handler in server.js
const pokemon_endpoint = ['abilities', 'moves', 'photo'];
const pokemon_species_endpoint = ['description', 'evolution'];
app.post("/pokedex", async (req, res) => {
try {
const { intent, parameters, outputContexts, queryText } = req.body.queryResult;
const pokemon = (parameters.pokemon) ? parameters.pokemon.toLowerCase().replace('.', '-').replace(' ', '').replace("'", "") : '';
const specs = parameters.specs;
@anchetaWern
anchetaWern / import server modules in server.js
Last active July 11, 2019 05:24
Pokedex Bot: import server modules
const express = require("express");
const bodyParser = require("body-parser");
const cors = require("cors");
const axios = require("axios");
require("dotenv").config();
const app = express();
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
@anchetaWern
anchetaWern / showResponse in App.js
Created July 11, 2019 05:22
Pokedex Bot: showResponse in App.js
showResponse(text, payload) {
let msg = {
_id: this.state.messages.length + 1,
text,
createdAt: new Date(),
user: BOT_USER
};
if (payload && payload.is_image) {
msg.text = text;
@anchetaWern
anchetaWern / handleResponse in App.js
Created July 11, 2019 05:21
Pokedex Bot: handleResponse in App.js
handleResponse(result) {
console.log(result);
let text = result.queryResult.fulfillmentMessages[0].text.text[0];
let payload = result.queryResult.webhookPayload;
this.showResponse(text, payload);
}
@anchetaWern
anchetaWern / onSend in App.js
Created July 11, 2019 05:21
Pokedex Bot: onSend in App.js
onSend(messages = []) {
this.setState(previousState => ({
messages: GiftedChat.append(previousState.messages, messages)
}));
let message = messages[0].text;
Dialogflow_V2.requestQuery(
message,
result => this.handleResponse(result),
error => console.log(error)
@anchetaWern
anchetaWern / render in App.js
Created July 11, 2019 05:20
Pokedex Bot: render in App.js
render() {
return (
<View style={styles.container}>
<GiftedChat
messages={this.state.messages}
onSend={messages => this.onSend(messages)}
user={{
_id: 1
}}
/>
@anchetaWern
anchetaWern / componentDidMount in App.js
Created July 11, 2019 05:19
Pokedex Bot: configure Dialogflow in App.js
componentDidMount() {
Dialogflow_V2.setConfiguration(
dialogflowConfig.client_email,
dialogflowConfig.private_key,
Dialogflow_V2.LANG_ENGLISH_US,
dialogflowConfig.project_id
);
}
@anchetaWern
anchetaWern / initialize bot in App.js
Created July 11, 2019 05:19
Pokedex Bot: Initialize bot in App.js
const BOT_USER = {
_id: 2,
name: 'Poke Bot',
avatar: 'https://ui-avatars.com/api/?background=0D8ABC&color=fff&name=Poke Bot'
};
class App extends Component {
state = {
messages: [
{
@anchetaWern
anchetaWern / import modules in App.js
Created July 11, 2019 05:18
Pokedex Bot: Import modules in App.js
import React, { Component } from 'react';
import { View, Text, StyleSheet } from 'react-native';
import { GiftedChat } from 'react-native-gifted-chat';
import { Dialogflow_V2 } from 'react-native-dialogflow';
import { dialogflowConfig } from './config';