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

mongoDB 除了可以让 line bot 使用之外,flask同样也可以去透过 pymongo 来取得资料,因此我们可以透过 line bot 将资料存到资料库中后,在让 flask 去资料库内取得资料并显示在网页,这样一来,使用者或管理员想要比较直观的看一些资料的时候,就可以直接开启网页观看。

範例程式码:

import pymongomyclient = 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_idexcept: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))

关于作者: 网站小编

码农网专注IT技术教程资源分享平台,学习资源下载网站,58码农网包含计算机技术、网站程序源码下载、编程技术论坛、互联网资源下载等产品服务,提供原创、优质、完整内容的专业码农交流分享平台。

热门文章