iT邦幫忙

2018 iT 邦幫忙鐵人賽
DAY 22
1
AI & Machine Learning

ChatBot&Chatbase系列 第 22

Day22[Line ChatBot]ChatBot x Google試算表

今天想介紹的是把使用者說的內容記錄到Google試算表

1.進入Google APIs網站,登入自己的google帳號
( 網站: https://console.developers.google.com/apis )
就會進到管理頁面

2.新增專案
https://ithelp.ithome.com.tw/upload/images/20180111/20107144iwHsqsKE9v.png

3.新增專案的頁面
取個名稱,並點選建立即可
https://ithelp.ithome.com.tw/upload/images/20180111/20107144e27WMC3amE.png

4.點選資料庫
https://ithelp.ithome.com.tw/upload/images/20180111/20107144zOm8jAYP8o.png

5.點選Google Drive API
https://ithelp.ithome.com.tw/upload/images/20180111/20107144vocRjKUJsT.png

6.點選啟用
https://ithelp.ithome.com.tw/upload/images/20180111/201071446ezQISpYlt.png

7.點選憑證
https://ithelp.ithome.com.tw/upload/images/20180111/201071444Qe2gucLx0.png

8.選擇建立一個 服務帳戶金鑰
https://ithelp.ithome.com.tw/upload/images/20180111/20107144YdjL5ET6RT.png

9.建立帳戶
選擇帳戶 > 建立新的服務帳戶
取個名稱之後 服務ID會自動產生
角色 > Project > 編輯者
金鑰類型選擇Json檔
並點選建立就完成了!!
https://ithelp.ithome.com.tw/upload/images/20180111/20107144llo13CO1XO.png

10.完成後會下載一個Json檔案
裡面包含金鑰資訊等等
!!注意!! 這個檔案需要跟執行的python檔放在同一個資料夾
https://ithelp.ithome.com.tw/upload/images/20180111/20107144ppVwcaIylk.png

11.新增一個Google試算表
內容的部分 > 留著第一行,第2-1000行一定要刪除!不然新增的內容會從1001行開始喔!
再來請選擇共用設定
https://ithelp.ithome.com.tw/upload/images/20180111/20107144u2FD7PtVun.png

12.在使用者的地方,填入Json檔中的client_email
權限要記得改為可編輯!
https://ithelp.ithome.com.tw/upload/images/20180111/201071447UqyRXdbx9.png
https://ithelp.ithome.com.tw/upload/images/20180111/20107144ULkaSkhWP7.png

13.進入python程式的部分
與Line的連結(收發訊息的部分) 是和以往做的都相同
這邊就不贅述
安裝並import需要的套件
$pip install oauth2client
$pip install gspread

import sys
import datetime
import gspread
from oauth2client.service_account import ServiceAccountCredentials as SAC

14.當Line輸入什麼就紀錄到google試算表,並回傳"紀錄成功"

if event.message.text != "":
        line_bot_api.reply_message(event.reply_token,TextSendMessage(text="紀錄成功"))
        pass
        #GDriveJSON就輸入下載下來Json檔名稱
        #GSpreadSheet是google試算表名稱
        GDriveJSON = 'LineBot.json'
        GSpreadSheet = 'BotTest'
        while True:
            try:
                scope = ['https://spreadsheets.google.com/feeds']
                key = SAC.from_json_keyfile_name(GDriveJSON, scope)
                gc = gspread.authorize(key)
                worksheet = gc.open(GSpreadSheet).sheet1
            except Exception as ex:
                print('無法連線Google試算表', ex)
                sys.exit(1)
            textt=""
            textt+=event.message.text
            if textt!="":
                worksheet.append_row((datetime.datetime.now(), textt))
                print('新增一列資料到試算表' ,GSpreadSheet)
                return textt                 

15.Demo
在Line上發送的訊息
https://ithelp.ithome.com.tw/upload/images/20180111/20107144LKqJPFekDd.jpg
google試算表紀錄收到的時間與內容
https://ithelp.ithome.com.tw/upload/images/20180111/20107144SDfIM1Gitg.png


上一篇
Day21[Line ChatBot]Line一下爬好爬滿(下)-Part.2
下一篇
Day23[Line ChatBot]Line ChatBot總結
系列文
ChatBot&Chatbase30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 則留言

1
denny60300
iT邦新手 5 級 ‧ 2018-03-29 16:55:46

TzuHui大大
想請教你要怎麼解決Linebot無法與google計算表連線的問題?
像是我把client email用在共享編輯人,
可是gmail會收到無法找到client email地址,
所以想問您怎麼解決。

sudjein iT邦新手 5 級 ‧ 2020-02-11 11:48:54 檢舉

oauth2client 這個有更新,https://github.com/adafruit/Adafruit_Python_DHT/blob/master/examples/google_spreadsheet.py
這個給你參考.修改好就可以了

我要留言

立即登入留言