<jupyter_start><jupyter_text><jupyter_code>import apiai
import json
import requests
from pymongo import MongoClient

############################  MONGODB INTEGRATION #################################

# mongoDB client
MONGODB_URI = os.environ.get("MONGODB_URI")
client = MongoClient(MONGODB_URI)
db = client.get_database("News")
news_records = db.news_records

def getRECORDS(user_id):
	"""
	function to fetch all news searches of a user
	"""
	records = news_records.find({"sender_id":user_id})
	return records

def pushRECORD(record):
	"""
	function to push news record to collection
	"""
	news_records.insert_one(record)
####################################################################################

# api.ai client 
APIAI_ACCESS_TOKEN = "APIAI_ACCESS_TOKEN"
ai = apiai.ApiAI(APIAI_ACCESS_TOKEN)

# endpoint of the news API
GNEWS_API_ENDPOINT = "https://gnewsapi.herokuapp.com"

# available news categories
news_categories = [('sports', 'sports news'), ('political', 'political news'), ('business', 'business news'), 
				   ('top stories', 'top stories news'), ('world', 'world news'), ('national', 'national news'), 
					('technology', 'technology news'), ('entertainment', 'entertainment news')]

# a help message
HELP_MSG = """
Hey! I am NewsBot. 
I can provide you news from all around the world in different languages, on different topics! 
Try any of these categories. :)
"""



def get_news(params):
	"""
	function to fetch news from news API
	"""
	params['news'] = params.get('news', "top stories")
	resp = requests.get(GNEWS_API_ENDPOINT, params = params)
	return resp.json()


def apiai_response(query, session_id):
	"""
	function to fetch api.ai response
	"""
	request = ai.text_request()
	request.lang='en'
	request.session_id=session_id
	request.query = query
	response = request.getresponse()
	return json.loads(response.read().decode('utf8'))


def parse_response(response):
	"""
	function to parse response and 
	return intent and its parameters
	"""
	result = response['result']
	params = result.get('parameters')
	intent = result['metadata'].get('intentName')
	return intent, params

	
def fetch_reply(query, session_id):
	"""
	main function to fetch reply for chatbot and 
	return a reply dict with reply 'type' and 'data'
	"""
	response = apiai_response(query, session_id)
	print(response)
	intent, params = parse_response(response)

	reply = {}

	
	if response['result']['action'].startswith('smalltalk'):
		reply['type'] = 'smalltalk'
		reply['data'] = response['result']['fulfillment']['speech']
		
	elif intent == "show_news":
		reply['type'] = 'news'

		params['sender_id'] = session_id
		# push news search record to mongoDB
		pushRECORD(params)

		articles = get_news(params)

		# create generic template
		news_elements = []

		for article in articles:
			element = {}
			element['title'] = article['title']
			element['item_url'] = article['link']
			element['image_url'] = article['img']
			element['buttons'] = [{
				"type":"web_url",
				"title":"Read more",
				"url":article['link']}]
			news_elements.append(element)

		reply['data'] = news_elements
	else:
		reply['type'] = 'none'
		reply['data'] = [{"type":"postback",
						  "payload": "SHOW_HELP",
						  "title":"Click here for help!"}]

	return reply<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1819841752891053, 'fraction_numerical': 0.00030432136335970786, 'mean_word_length': 5.249049429657795, 'pattern_counts': {'":': 8, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '118895', 'license': 'MIT', 'n_tokens_mistral': 1109, 'n_tokens_neox': 1029, 'n_words': 263, 'path': 'Utils.ipynb', 'repo_name': 'Ugo5738/Facebook-Messenger-Bot', 'size': '3286'}