-
Notifications
You must be signed in to change notification settings - Fork 16
/
utils.py
46 lines (32 loc) · 985 Bytes
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
from wit import Wit
from gnewsclient import gnewsclient
access_token = "server_access_token_here"
client = Wit(access_token = access_token)
def wit_response(message_text):
resp = client.message(message_text)
categories = {'newstype':None, 'location':None}
entities = list(resp['entities'])
for entity in entities:
categories[entity] = resp['entities'][entity][0]['value']
return categories
def get_news_elements(categories):
news_client = gnewsclient()
news_client.query = ''
if categories['newstype'] != None:
news_client.query += categories['newstype'] + ' '
if categories['location'] != None:
news_client.query += categories['location']
news_items = news_client.get_news()
elements = []
for item in news_items:
element = {
'title': item['title'],
'buttons': [{
'type': 'web_url',
'title': "Read more",
'url': item['link']
}],
'image_url': item['img']
}
elements.append(element)
return elements