Linebot Django 8

less than 1 minute read

LINE BOT Message 初學者記錄(八)  Casual template: Yelp restaurant + Location 為了能在各國旅遊時可以方便找到優質餐廳, 結合 Location 和 Yelp 即可輕鬆達成

先看作品完成圖: 不管德國, 台灣, 美國… 都可輕鬆取得優質餐廳資訊

        

Step1 - Distinguish message from user is Location or Text:Permalink

從上圖看出使用者輸入 location 會有一個 type: location, 因此我們只需要在 main function 裡判斷來源字串的 message_type 即可 if message_type == 'location':

Step2 - Yelp function:Permalink

我們 github 可看到現成的 yelp api in Python language. 並將吃進去的 address 最後 filter 出我們要的 ‘name’, ‘address’, ‘photo’, ‘url’

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import argparse
import json
import pprint
import requests
import sys
try:
# For Python 3.0 and later
from urllib.error import HTTPError
from urllib.parse import quote
from urllib.parse import urlencode
except ImportError:
# Fall back to Python 2's urllib2 and urllib
from urllib2 import HTTPError
from urllib import quote
from urllib import urlencode
# OAuth credential placeholders that must be filled in by users.
# You can find them on
# https://www.yelp.com/developers/v3/manage_app
CLIENT_ID = 'xxxxxxxxx'
CLIENT_SECRET = 'xxxxxxxxx'
# API constants, you shouldn't have to change these.
API_HOST = 'https://api.yelp.com'
SEARCH_PATH = '/v3/businesses/search'
BUSINESS_PATH = '/v3/businesses/' # Business ID will come after slash.
TOKEN_PATH = '/oauth2/token'
GRANT_TYPE = 'client_credentials'
# Defaults for our simple example.
DEFAULT_TERM = 'restaurant'
DEFAULT_LOCATION = 'No. 1, Section 4, Roosevelt Rd, Da’an District, Taipei City, Taiwan 10617'
SEARCH_LIMIT = 5
def obtain_bearer_token(host, path):
url = '{0}{1}'.format(host, quote(path.encode('utf8')))
assert CLIENT_ID, "Please supply your client_id."
assert CLIENT_SECRET, "Please supply your client_secret."
data = urlencode({
'client_id': CLIENT_ID,
'client_secret': CLIENT_SECRET,
'grant_type': GRANT_TYPE,
})
headers = {
'content-type': 'application/x-www-form-urlencoded',
}
response = requests.request('POST', url, data=data, headers=headers)
bearer_token = response.json()['access_token']
return bearer_token
def request(host, path, bearer_token, url_params=None):
url_params = url_params or {}
url = '{0}{1}'.format(host, quote(path.encode('utf8')))
headers = {
'Authorization': 'Bearer %s' % bearer_token,
}
response = requests.request('GET', url, headers=headers, params=url_params)
return response.json()
def search(bearer_token, term, location):
url_params = {
'term': term.replace(' ', '+'),
'location': location.replace(' ', '+'),
'limit': SEARCH_LIMIT,
'radius': 1000
}
return request(API_HOST, SEARCH_PATH, bearer_token, url_params=url_params)
def get_restaurant(term, location):
bearer_token = obtain_bearer_token(API_HOST, TOKEN_PATH)
response = search(bearer_token, term, location)
businesses = response.get('businesses')
restaurants = []
for business in businesses:
restaurant = {}
restaurant['name'] = business['name']
restaurant['address'] = business['location']['display_address'][0]
restaurant['photo'] = business['image_url']
restaurant['yelp_url'] = business['url']
restaurants.append(restaurant)
return restaurants
view raw beginner-8 hosted with ❤ by GitHub

Step3 - Apply Yelp result to LINE:Permalink

使用 LINE BOT Message 初學者記錄(三)    News 相同 casual template 方法 可以先檢查一下 payload data 符合我們預期的

就完成喽!! 現在只要丟出 Location 就可得到附近優質餐廳推薦!!

 

南瓜出沒 掃描 QR Code 專區Permalink

剛好看到有興趣試試可以加入玩一下

What’s next:Permalink

  1. 結合 GPS 定位找飲料店
  2. 學別人上傳照片到 Google Cloud
  3. 結合 Spread Sheet 來記帳看起來不錯, 但我不記帳的 >< 
  4. 英文選擇題練習給認真的學生, 可惜沒看到 Free API
  5. 結合 ML 套件玩分析

2017/9/30

下一篇:  Line Bot - Dialogflow

Leave a comment