python cloud functions linebot作成

実現したいこと

linebotにて英単語を出題するbot作成

前提

環境
GCP(cloudfunctions)
英単語取得
スプレットシート
言語
python

現在
https://qiita.com/takuya0125/items/06696486f323b9d7ceeb
上記サイトを見ながら作成していますがうまくできないです。
上記サイトでは、スプシを使用していないのでちょっと違うところあり
また、別サイトのlinebotオウム返しを作成した後、英単語出題botに改良中です。

問題とラインで送信すると
問題:「Australia」の意味を選択してください
「オーストラリア」
「~に」
「料理人」
「日本語」
上記のように出題はできるようになりました

発生している問題・エラーメッセージ

選択肢を入力しても何も帰ってこない状態です

該当のソースコード

python

1import gspread 2from google.oauth2.service_account import Credentials 3 4import os 5import base64, hashlib, hmac 6import logging 7import random 8 9from flask import abort, jsonify 10 11from linebot import (12 LineBotApi, WebhookParser 13)14from linebot.exceptions import (15 InvalidSignatureError 16)17from linebot.models import (18 MessageEvent, TextMessage, TextSendMessage 19)20 21 22def spshe_get():23 scope = ['https://spreadsheets.google.com/feeds','https://www.googleapis.com/auth/drive']24 #認証設定25 credentials = Credentials.from_service_account_file('word_write.json', scopes=scope)26 27 #OAuth2の資格を使用しログイン28 gc = gspread.authorize(credentials)29 30 #スプレットシートキー31 SPREADSHEET_KEY = "ABCDEFG"32 workbook = gc.open_by_key(SPREADSHEET_KEY)33 worksheet = workbook.worksheet('中学一年生単語一覧')34 return worksheet.get_all_values()35 36def main(request):37 channel_secret = os.environ.get('LINE_CHANNEL_SECRET')38 channel_access_token = os.environ.get('LINE_CHANNEL_ACCESS_TOKEN')39 40 line_bot_api = LineBotApi(channel_access_token)41 parser = WebhookParser(channel_secret)42 43 body = request.get_data(as_text=True)44 hash = hmac.new(channel_secret.encode('utf-8'),45 body.encode('utf-8'), hashlib.sha256).digest()46 signature = base64.b64encode(hash).decode()47 48 if signature != request.headers['X_LINE_SIGNATURE']:49 return abort(405)50 51 try:52 events = parser.parse(body, signature)53 except InvalidSignatureError:54 return abort(405)55 56 # #スプシから英単語取得関数実行57 all_date = spshe_get()58 question_message = question(all_date)59 60 for event in events:61 if not isinstance(event, MessageEvent):62 continue63 if not isinstance(event.message, TextMessage):64 continue65 66 if event.message.text == '問題':67 line_bot_api.reply_message(68 event.reply_token,69 TextSendMessage(question_message[1])70 )71 72 elif event.message.text == question_message[1][1]:73 line_bot_api.reply_message(74 event.reply_token,75 TextSendMessage('正解')76 )77 78 elif event.message.text == 'a':79 line_bot_api.reply_message(80 event.reply_token,81 TextSendMessage('違います。。。。\n正解は「{}」です'.formatquestion_message[1][1])82 )83 84 return jsonify({ 'message': 'ok'})85 86def question(all_date):87 88 ja_word = []#日本語意味格納変数89 en_word = []#英単語格納変数90 words = random.sample(all_date,4)91 92 93 for word in words:94 en_word.append(word[0])95 ja_word.append(word[1])96 97 #4つの単語からランダムに一つ取得する98 question_word = random.choice(words)99 100 question_message = ('問題:「{}」の意味を選択してください\n「{}」\n「{}」\n「{}」\n「{}」'.format(question_word[0],ja_word[0],ja_word[1],ja_word[2],ja_word[3]),question_word)101 return question_message 102

コメントを投稿

0 コメント