Python勉強会 第6回
<第6回Web会議日程>
2024年04月17日(水) 19:00から
0.目次
1.AWSを利用したLine連携の仕組み(参考情報)
2.課題内容
3.Lambda関数の編集
4.Lambda関数のログ確認
1.AWSを利用したLine連携の仕組み(参考情報)
Image Not Showing
Possible Reasons
- The image was uploaded to a note which you don't have access to
- The note which the image was originally uploaded to has been deleted
Learn More →
①個人のスマホからLine公式アカウントにメッセージ送信
②Line公式からWebhookしたAPI GatewayのURLへメッセージを送信
③API Gatewayに関連付けられたLambdaを呼び出し
④Lambdaで作り込んだ処理に沿ってメッセージを応答
2.課題内容
課題ではAWS LambdaやLineを利用します。
これらをPythonで利用するための固有の知識についてまずは記載します。
・「lambda_handler」関数と「lambda_function.py」ファイル
「最初に処理をする関数」と「その関数が記載されたファイル名」です。
今回はデフォルトの名前で用意しています。
「fm_pys_XXX」フォルダ直下に「lambda_function.py」ファイルがあり、
その22行目に「lambda_handler」関数が確認できると思います。
lambda関数を呼び出すと、処理はそこから動き始めます。
・「evnet」変数
lambda_handler関数の第1引数で、関数を呼び出す際に様々な情報が格納されます。
Lineから送信したメッセージもこの中に格納されます。
json形式で格納されているため、送信メッセージを取り出すためには以下のように指定します。
json.loads(event['body'])['events'][0]['message']['text']
・Lineへの応答
Lineへの応答は特定のURLへ情報を送信することで実現しています。
15~20行目でURLやメソッド、ヘッダを定義。
33~35行目で応答メッセージとするパラメータを定義。
49~51行目でパラメータに文字列の値を設定。
58行目で設定した情報をLINEに送信。
応答メッセージを変更したい場合は、51行目を変更することで対応できます。
課題
if文を利用して受信メッセージを判定し、応答メッセージを変更する
・if文について ※おさらいの内容
<if文の基本構成>
上記例では、最初に条件式1が評価されます。
条件式1がTrueの場合、条件1がTrueの場合に実行するコードが実行されます。
条件式1がFalseの場合、次に条件式2が評価されます。
条件式2がTrueの場合、条件2がTrueの場合に実行するコードが実行されます。
条件式2がFalseの場合、elseブロックのコードが実行されます。
注意事項
・Lineで送信したメッセージは講師には見えてしまうため、私的な内容の送信は控えてください。
・Lambda関数がサポートしているPythonは3.9のため、match文は利用できません。
match文について
3.Lambda関数の編集
3-1.AWSへログイン
※ログイン情報は別紙参照
Image Not Showing
Possible Reasons
- The image was uploaded to a note which you don't have access to
- The note which the image was originally uploaded to has been deleted
Learn More →
3-2.上部メニューより「lambda」を検索して選択
Image Not Showing
Possible Reasons
- The image was uploaded to a note which you don't have access to
- The note which the image was originally uploaded to has been deleted
Learn More →
3-3.左メニューから「関数」を選択
表示された一覧から自分のLambda関数をクリック
Image Not Showing
Possible Reasons
- The image was uploaded to a note which you don't have access to
- The note which the image was originally uploaded to has been deleted
Learn More →
3-4.「コード」より「lambda_function.py」を編集
Image Not Showing
Possible Reasons
- The image was uploaded to a note which you don't have access to
- The note which the image was originally uploaded to has been deleted
Learn More →
3-5.編集が完了した「File」メニューから「Save」を選択
Image Not Showing
Possible Reasons
- The image was uploaded to a note which you don't have access to
- The note which the image was originally uploaded to has been deleted
Learn More →
3-6.「Deploy」をクリックし、「正常に更新されました」のメッセージを確認
Image Not Showing
Possible Reasons
- The image was uploaded to a note which you don't have access to
- The note which the image was originally uploaded to has been deleted
Learn More →
Image Not Showing
Possible Reasons
- The image was uploaded to a note which you don't have access to
- The note which the image was originally uploaded to has been deleted
Learn More →
4.Lambda関数のログ確認
Lambda関数のログは「モニタリング」から確認することが出来る
また「logger.info(文字列)」で任意の内容を出力することも出来る
4-1.logger.infoで出力を設定
Image Not Showing
Possible Reasons
- The image was uploaded to a note which you don't have access to
- The note which the image was originally uploaded to has been deleted
Learn More →
4-2.Deployしてからスマホからメッセージを送信
4-3.「モニタリング」から「ログ」を選択し、一覧から実行時間のログを選択
(表示はUTCのため、9時間遅れとなっていることに注意)
Image Not Showing
Possible Reasons
- The image was uploaded to a note which you don't have access to
- The note which the image was originally uploaded to has been deleted
Learn More →
4-4.想定通りのメッセージが出力されている
Image Not Showing
Possible Reasons
- The image was uploaded to a note which you don't have access to
- The note which the image was originally uploaded to has been deleted
Learn More →
4-5.構文エラーがある場合は以下のように出力される
(以下は23行目に構文エラー(Syntax error)があると出力されている)
Image Not Showing
Possible Reasons
- The image was uploaded to a note which you don't have access to
- The note which the image was originally uploaded to has been deleted
Learn More →