LocationMessage、LocationSendMessage from linebot.models import (
MessageEvent, TextMessage, TextSendMessage,
LocationMessage, LocationSendMessage,
)
Location Handler
@handler.add(MessageEvent, message=LocationMessage)
def handle_location_message(event):
now_lat=event.message.latitude
now_lng=event.message.longitude
reply = "現在位置:緯度{},經度{}".format(now_lat,now_lng)
line_bot_api.reply_message(
event.reply_token,
TextSendMessage(text=reply)
)
資料庫 > Google Maps API > 更多 > Google Places API Web Service > 啟用

從文件中找找看,
這個回傳格式叫做json,一種資料交換格式的語言,
但在解析之前,大家觀察一下Google Place API回傳的資料,
還記得上禮拜教的串列是以什麼符號取索引嗎?
這次出現了新的符號{}
我們先來介紹Python中同樣也使用{}的「字典」
my_dict = {}
my_dict["the"] = 10
my_dict["a"] = 9
my_dict["of"] = 6
my_dict["in"] = 6
my_dict2 = {"the":10,"a":9,"of":6,"in":6}
print (my_dict)
print (my_dict2)
print (my_dict["the"])
{'the': 10, 'in': 6, 'of': 6, 'a': 9}
{'the': 10, 'in': 6, 'of': 6, 'a': 9}
10
my_dict = {"the":10,"a":9,"of":6,"in":6}
print ("with" in my_dict)
print (my_dict["with"])
False
--------------------------------------------------------------------------- KeyError Traceback (most recent call last) <ipython-input-2-bfa4e840c783> in <module>() 1 my_dict = {"the":10,"a":9,"of":6,"in":6} 2 print ("with" in my_dict) ----> 3 print (my_dict["with"]) KeyError: 'with'
json.loads(字串):json格式字串 -> python字典型態json.dumps(字典):python字典型態 -> json格式字串 輔助工具:Json Parser Online
右鍵>檢查>Network

import requests
import json
url = "https://maps.googleapis.com/maps/api/place/autocomplete/json?input=女人迷樂園&key=你的金鑰"
rep = requests.get(url) # 回傳的Response物件,包含Header、網頁原始碼
html = rep.text # Response物件,網頁原始碼的部分
json_data = json.loads(html)
print (json_data['predictions'][0]['description'])
台灣台北市大安區和平東路二段女人迷樂園 womany wonderland
網路爬蟲就是這樣運作的喔!
有興趣可以參考PyLadies之前「從0到1的網頁爬蟲」活動01~07的投影片:
import requests
import json
url = "https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=25.025315,121.5295463&key=你的金鑰&rankby=distance&types=police&language=zh-TW"
rep = requests.get(url)
html = rep.text
data = json.loads(html)
first = data['results'][0]
title = first['name']
print (title)
和平東路派出所
與 Line 的 LocationSendMessager 結合,找尋附近的警察局

import requests
import json
session_id ="COPY CURL"
client_ac = "Client access token"
url = "https://api.api.ai/v1/query?query={}&lang=zh-TW&sessionId={}".format("我有問題",session_id)
header={
"Authorization":"Bearer {}".format(client_ac)
}
rep = requests.get(url,headers=header)
html = rep.text
print (html)
{
"id": "9f8df4b5-46ba-41d9-affc-27b69d8913b4",
"timestamp": "2017-08-13T02:40:33.175Z",
"lang": "zh-cn",
"result": {
"source": "agent",
"resolvedQuery": "我有問題",
"speech": "請問你想找哪裡的資訊?",
"action": "",
"parameters": {
"help": "問題",
"POI": ""
},
"metadata": {
"inputContexts": [],
"outputContexts": [],
"intentName": "Ask for help",
"intentId": "5c9ef3c0-cd86-4bc8-9333-2004bb9997fd",
"webhookUsed": "false",
"webhookForSlotFillingUsed": "false",
"contexts": [
"5c9ef3c0-cd86-4bc8-9333-2004bb9997fd_id_dialog_context",
"ask_for_help_dialog_params_poi",
"ask_for_help_dialog_context"
]
},
"score": 1.0
},
"status": {
"code": 200,
"errorType": "success"
},
"sessionId": "8874573d-ad1f-43e1-bb20-8d00e8a0b9a8"
}
哪裡還可以學習講師的課程?
希望可以學到自己用flask開RESTful API
自動讀取圖檔上的文字並分類
這次寫的code是否可以轉移用在messenger bot
因為各家Bot的規格不同,所以是無法完全轉移的,
但同樣都是Flask框架,還有Python語法、串接api的部分是可行的!
可以參考 enginebai 寫的 FB Messenger Bot
Bot開發推薦網站