Created
May 30, 2017 10:22
-
-
Save mcdlee/7b43a7225b278111d39cfdaa75835b96 to your computer and use it in GitHub Desktop.
LineBot for Kaohsiung C-boke
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# encoding: utf-8 | |
from flask import Flask, request, abort | |
from linebot import ( | |
LineBotApi, WebhookHandler | |
) | |
from linebot.exceptions import ( | |
InvalidSignatureError | |
) | |
from linebot.models import ( | |
MessageEvent, TextMessage, TextSendMessage, LocationMessage, LocationSendMessage, StickerMessage, StickerSendMessage | |
) | |
import random | |
import datetime | |
import os | |
app = Flask(__name__) | |
handler = WebhookHandler('Your Channel Secret') | |
line_bot_api = LineBotApi('Your Channel Access Token') | |
@app.route('/') | |
def index(): | |
return "<p>Hello World!</p>" | |
@app.route("/callback", methods=['POST']) | |
def callback(): | |
# get X-Line-Signature header value | |
signature = request.headers['X-Line-Signature'] | |
# get request body as text | |
body = request.get_data(as_text=True) | |
app.logger.info("Request body: " + body) | |
# handle webhook body | |
try: | |
handler.handle(body, signature) | |
except InvalidSignatureError: | |
abort(400) | |
return 'OK' | |
# ================= 給經緯度時, 機器人區塊 Start ================= | |
@handler.add(MessageEvent, message=LocationMessage) # default | |
def handle_text_message(event): # default | |
#msg = event.message.text #message from user | |
from lxml import etree | |
from urllib.request import urlopen | |
from pandas import DataFrame | |
def getTable(): | |
url = 'http://www.c-bike.com.tw/xml/stationlistopendata.aspx' | |
response = urlopen(url) | |
xml = response.read() | |
tree = etree.XML(xml) | |
#db = [["StationID", "StationNO", "name", "available", "empty", "lat", "lon"]] #list in list | |
db = [] | |
for i in range(0, len(tree[0])): | |
StationID = tree[0][i].find('StationID').text | |
StationNO = tree[0][i].find('StationNO').text | |
name = tree[0][i].find('StationName').text | |
available = int(tree[0][i].find('StationNums1').text) | |
empty = int(tree[0][i].find('StationNums2').text) | |
lat = float(tree[0][i].find('StationLat').text) | |
lon = float(tree[0][i].find('StationLon').text) | |
print(name) | |
db.append([StationID, StationNO, name, available, empty, lat, lon]) | |
return(db) | |
lat = event.message.latitude | |
lon = event.message.longitude | |
db = DataFrame(getTable(), columns=("StationID", "StationNO", "name", "available", "empty", "lat", "lon")) | |
## 因為赤道和經線 1度大約110 km ,沒有考慮 cos 經度,正負 0.01 度大約正負1公里 | |
lat_min, lat_max = lat - 0.01, lat + 0.01 | |
lon_min, lon_max = lon - 0.01, lon + 0.01 | |
elective = db[(db['lat']>lat_min) & (db['lat']<lat_max) & (db['lon']>lon_min) & (db['lon']<lon_max)] | |
elective = elective.assign(distance = lambda x: ((x.lat-lat)**2 + (x.lon-lon)**2)) ##沒有考慮緯度的變形 | |
elective = elective.sort_values('distance') | |
reply = ('您給定的經緯度是%.3f, %.3f\n'%(lon, lat)) | |
for index, station in elective.iterrows(): | |
reply = reply + ('%s:空位%d,剩餘車輛%d\n'% (station['name'], station['empty'], station['available'])) | |
# 針對使用者各種訊息的回覆 Start ========= | |
line_bot_api.reply_message( | |
event.reply_token, | |
TextSendMessage(text=reply) | |
) | |
# 針對使用者各種訊息的回覆 End ========= | |
# ================= 機器人區塊 End ================= | |
# ================= 給文字時,機器人區塊 Start ================= | |
@handler.add(MessageEvent, message=TextMessage) # default | |
def handle_text_message(event): # default | |
msg = event.message.text #message from user | |
reply = msg | |
# 針對使用者各種訊息的回覆 Start ========= | |
line_bot_api.reply_message( | |
event.reply_token, | |
TextSendMessage(text=reply) #應聲蟲一隻 | |
) | |
# 針對使用者各種訊息的回覆 End ========= | |
# ================= 機器人區塊 End ================= | |
import os | |
if __name__ == "__main__": | |
app.run(host='0.0.0.0',port=os.environ['PORT']) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
web: python app.py |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
line-bot-sdk | |
flask | |
lxml | |
pandas | |
urllib3==1.13.1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
python-3.5.2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment