Created
July 27, 2020 08:24
-
-
Save varunngoyal/83592f80f5f177a357df8e9eb8f10ed6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# import flask dependencies | |
from flask import Flask, request, make_response, jsonify | |
import pandas as pd | |
from df_msg_library import * | |
# initialize the flask app | |
app = Flask(__name__) | |
df = pd.read_csv("Country_Flags.csv") | |
# default route | |
@app.route('/') | |
def index(): | |
return 'Hello World!' | |
# function for responses | |
def get_data(): | |
# build a request object | |
req = request.get_json(force=True) | |
# fetch action from json | |
action = req.get('queryResult').get('action') | |
if action == 'get_country_list': | |
res = get_countries(req) | |
elif action == 'view_flag': | |
res = get_flag(req) | |
elif action == 'view_more_flags': | |
res = get_countries(req) | |
return res | |
def get_flag(req): | |
lib = df_rich_responses() | |
country = req.get('queryResult').get( | |
'parameters').get('geo-country') | |
country = str(country) | |
print(country) | |
country_index = 0 | |
for i in range(len(df['Country'])): | |
if df['Country'][i].find(country) != -1: | |
country_index = i | |
break | |
country_name = str(df['Country'][country_index]) | |
country_url = str(df['ImageURL'][country_index]) | |
print(country_name, country_url) | |
response = lib.image_response(country_name, country_url) | |
return lib.fulfillment_messages("", response) | |
def get_countries(req): | |
message = "Hi there, here you can find flag of any county." + \ | |
"Click on one of the countries to see it's flag" | |
lib = df_rich_responses() | |
element = req.get('queryResult').get( | |
'parameters').get('geo-country') | |
start = 0 | |
if element != None: | |
# calculate start | |
for i in range(len(df['Country'])): | |
if df['Country'][i].find(element) != -1: | |
start = i+1 | |
break | |
countryList = df['Country'].astype(str).values.tolist() | |
countryList = countryList[start:start+5] | |
lastCountry = str(df['Country'][start+4]) | |
lastCountry = 'View more after '+lastCountry | |
countryList.append(lastCountry) | |
print(countryList) | |
response = lib.list_response(countryList) | |
return lib.fulfillment_messages(message, [response]) | |
# create a route for webhook | |
@app.route('/webhook', methods=['POST']) | |
def webhook(): | |
# return response | |
return make_response(jsonify(get_data())) | |
# run the app | |
if __name__ == '__main__': | |
app.run(port=8000, debug=True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment