Skip to content

Added new feature of Cricket #393

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 88 additions & 0 deletions code.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import json
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this file needed?

import os
import sys

import keen
import requests

import config
import modules
from src import *
from templates.quick_replies import add_quick_reply
from templates.text import TextTemplate

WIT_AI_ACCESS_TOKEN = os.environ.get('WIT_AI_ACCESS_TOKEN', config.WIT_AI_ACCESS_TOKEN)


def generate_postback(module):
return {
'intent': module,
'entities': None
}


def process_query(input):
# For local testing, mock the response from Wit
with open(config.WIT_LOCAL_DATA) as wit_file:
wit_local_data = json.load(wit_file)
if input.lower() in wit_local_data:
return wit_local_data[input.lower()]['intent'], wit_local_data[input.lower()]['entities']
try:
r = requests.get('https://api.wit.ai/message?v=20160420&q=' + input, headers={
'Authorization': 'Bearer %s' % WIT_AI_ACCESS_TOKEN
})
data = r.json()
intent = data['outcomes'][0]['intent']
entities = data['outcomes'][0]['entities']
confidence = data['outcomes'][0]['confidence']
if intent in src.__all__ and confidence > 0.5:
return intent, entities
else:
return None, {}
except:
return None, {}


def search(input, sender=None, postback=False):
if postback:
payload = json.loads(input)
intent = payload['intent']
entities = payload['entities']
else:
intent, entities = process_query(input)
# TODO: Needs to be refactored out
try:
keen.project_id = os.environ.get('KEEN_PROJECT_ID', config.KEEN_PROJECT_ID)
keen.write_key = os.environ.get('KEEN_WRITE_KEY', config.KEEN_WRITE_KEY)
keen.add_event('logs', {
'intent': intent,
'entities': entities,
'input': input,
'sender': sender,
'postback': postback
})
except:
pass # Could not stream data for analytics
if intent is not None:
if intent in src.__personalized__ and sender is not None:
r = requests.get('https://graph.facebook.com/v2.6/' + str(sender), params={
'fields': 'first_name',
'access_token': os.environ.get('ACCESS_TOKEN', config.ACCESS_TOKEN)
})
if entities is None:
entities = {}
entities['sender'] = r.json()
data = sys.modules['modules.src.' + intent].process(input, entities)
if data['success']:
return data['output']
else:
if 'error_msg' in data:
return data['error_msg']
else:
return TextTemplate('Something didn\'t work as expected! I\'ll report this to my master.').get_message()
else:
message = TextTemplate(
'I\'m sorry; I\'m not sure I understand what you\'re trying to say.\nTry typing "help" or "request"').get_message()
message = add_quick_reply(message, 'Help', modules.generate_postback('help'))
message = add_quick_reply(message, 'Request', modules.generate_postback('request'))
return message
4 changes: 4 additions & 0 deletions local/wit.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,9 @@
"test": {
"intent": "xkcd",
"entities": []
},
"cricket": {
"intent": "cricket",
"entities": []
}
}
5 changes: 5 additions & 0 deletions messenger/persistent_menu.json
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@
"title": "videos of sia",
"payload": "{\"entities\": {\"video\":[{\"value\":\"sia\"}]}, \"intent\": \"video\"}"
}
{
"type": "postback",
"title": "cricket livescores",
"payload": "{\"entities\": null, \"intent\": \"cricket\"}"
}
]
},
{
Expand Down
1 change: 1 addition & 0 deletions modules/src/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
'book',
'bye',
'coin',
'cricket',
'currency',
'dice',
'dictionary',
Expand Down
32 changes: 32 additions & 0 deletions modules/src/cricket.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
from pycricbuzz import Cricbuzz
import requests
import requests_cache
from templates.button import *


def process(input, entities=None):
output = {}
try:
cricket = Cricbuzz()
matches = cricket.matches()
text_data=""
template = TextTemplate()
for match in matches:
cric_data=cricket.livescore(match['id'])
text_data+=cric_data['matchinfo']['mnum']+"\n"+cric_data['matchinfo']['mchdesc']+"\n"
text_data+=cric_data['batting']['team']+"\t"+cric_data['batting']['score'][0]['runs']+"/"+cric_data['batting']['score'][0]['wickets']+"\n"
text_data+=cric_data['bowling']['team']+"\t"+cric_data['bowling']['score'][0]['runs']+"/"+cric_data['bowling']['score'][0]['wickets']+"\n"
text_data+=cric_data['matchinfo']['status']+"\n"+"*"*10+"\n"
template.set_text(text_data)
text = template.get_text()
template = ButtonTemplate(text)
template.add_web_url('CricBuzz Live', 'http://www.cricbuzz.com/cricket-match/live-scores')

output['input'] = input
output['output'] = template.get_message()
output['success'] = True
except:
error_message = 'There was some error while retrieving data from Cricbuzz'
output['error_msg'] = TextTemplate(error_message).get_message()
output['success'] = False
return output
8 changes: 8 additions & 0 deletions modules/tests/test_cricket.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import modules

def test_cricket():
assert ('cricket' == modules.process_query('cricket')[0])
assert ('cricket' == modules.process_query('live cricket')[0])
assert ('cricket' == modules.process_query('cricket livescores')[0])
assert ('cricket' != modules.process_query('something random')[0])

1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ pbr==3.1.1
py==1.4.34
pyasn1==0.1.9
pycparser==2.14
pycricbuzz==0.8
pycryptodome==3.4.7
PyDispatcher==2.0.5
pyOpenSSL==16.0.0
Expand Down