請問有辦法在line bot中輸入「更新」,然後背後的指令是讓python去更新google sheet嗎?
因為我在python中已經寫好程式碼,也確認可以執行,但想跟line bot結合就無法執行了
懇請各位幫忙,感謝!!
*補------------------
我是用python寫的
這我的程式碼,放在stock_title.py中,單獨在python中執行是可以的
import gspread
from oauth2client.service_account import ServiceAccountCredentials as SAC
import requests
from bs4 import BeautifulSoup
from datetime import datetime
def stock_name():
Json = 金鑰
Url = ['https://spreadsheets.google.com/feeds']
Connect = SAC.from_json_keyfile_name(Json, Url)
GoogleSheets = gspread.authorize(Connect)
Sheet = GoogleSheets.open_by_key('google試算表代號')
Sheets = Sheet.sheet1
url = "https://isin.twse.com.tw/isin/class_main.jsp?owncode=&stockname=&isincode=&market=1&issuetype=1&industry_code=&Page=1&chklike=Y"
res = requests.get(url)
soup = BeautifulSoup(res.text,"html.parser")
soup1 = soup.find("table",{"class":"h4"})
url2 = "https://isin.twse.com.tw/isin/class_main.jsp?owncode=&stockname=&isincode=&market=2&issuetype=4&industry_code=&Page=1&chklike=Y"
res2 = requests.get(url2)
soup2 = BeautifulSoup(res2.text,"html.parser")
soup3 = soup2.find("table",{"class":"h4"})
s_id = []
for i in range(len(soup1.find_all("tr"))):
s_id_ = soup1.find_all("tr")[i].text.split("\n")[3:5]
s_id.append(s_id_)
for i in range(len(soup3.find_all("tr"))):
s_id_ = soup3.find_all("tr")[i].text.split("\n")[3:5]
s_id.append(s_id_)
if len(Sheets.get_all_records()) == 0:
Sheets.append_rows(s_id)
else:
Sheets.update(s_id)
return "{} 更新完成".format(datetime.now().strftime('%Y-%m-%D %H:%M:%S'))
然後line bot的程式碼中我新增了下面這段(elif前面是有其他的關鍵字指令),但line bot輸入更新後只有已讀沒有其他反映,輸入其他的關鍵字指令還是跑得出結果,就只有這段不行
from stock_title import *
elif re.match('更新',message):
update = stock_name()
line_bot_api.reply_message(
event.reply_token,TextSendMessage(update))
update = stock_name() 拿掉
event.reply_token,TextSendMessage(stock_title.stock_name()) 這樣呢?