iT邦幫忙

0

用 Python 暢玩 Line bot - 24:Flask(二)

  • 分享至 

  • twitterImage
  •  

mongoDB 除了可以讓 line bot 使用之外,flask同樣也可以去透過 pymongo 來取得資料,因此我們可以透過 line bot 將資料存到資料庫中後,在讓 flask 去資料庫內取得資料並顯示在網頁,這樣一來,使用者或管理員想要比較直觀的看一些資料的時候,就可以直接開啟網頁觀看。

範例程式碼:

import pymongo

myclient = pymongo.MongoClient(<connect string>)
mydb = myclient[<dbname>] # 指定資料庫
players = mydb["player"] # 指定資料表

@app.route("/")
def index():
	player_information = players.find({ "PlayerId":"Ub051f25d61a6741ff6f103051dd11aac" })
	for i in player_information:
		message = i["PlayerName"]
	return message

前面也有提過 LIFF 的建立法,如果不希望使用者跳出 line 的介面的話,我們也可以把網址轉成 LIFF 的格式在傳給使用者。

def GetLIFF(message, url):
	if(message == "LIFF"):
		try:
			liff_id = liff_api.add(view_type="tall", view_url=url)
			return 'https://liff.line.me/'+liff_id
		except:
			print(err.message)

@handler.add(MessageEvent)
def handle_message(event):
	text = GetLIFF("LIFF", "https://f6a3-1-171-232-38.ngrok.io/")
	line_bot_api.reply_message(event.reply_token, TextSendMessage(text=text))

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言