設定
ハンドラはファイル名.関数名
で指定する
SlackでImcomingWebHookを追加する
コード
- ググってでてくるrequestsというモジュールは、urllibの中に吸収されたみたい
- jsonをslackのWEBHOOK用のアドレスにPOSTで投げる
def lambda_handler(event, context):
のevent
はLambda実行時に関数へ引き渡されるJSONデータ- 実行元のリソースによってもちろん中身は違うが、JSONデータなのでテンプレート的に扱うことができる
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
<span class="kn">import</span> <span class="nn">json</span> <span class="kn">import</span> <span class="nn">urllib.request</span> <span class="k">def</span> <span class="nf">post_slack</span><span class="p">():</span> <span class="n">send_data</span> <span class="o">=</span> <span class="p">{</span> <span class="s">"username"</span><span class="p">:</span> <span class="s">"notify_slack"</span><span class="p">,</span> <span class="s">"icon_emoji"</span><span class="p">:</span> <span class="s">":vampire:"</span><span class="p">,</span> <span class="s">"text"</span><span class="p">:</span> <span class="s">"test"</span><span class="p">,</span> <span class="p">}</span> <span class="n">send_text</span> <span class="o">=</span> <span class="s">"payload="</span> <span class="o">+</span> <span class="n">json</span><span class="p">.</span><span class="n">dumps</span><span class="p">(</span><span class="n">send_data</span><span class="p">)</span> <span class="n">method</span> <span class="o">=</span> <span class="s">'POST'</span> <span class="n">headers</span> <span class="o">=</span> <span class="p">{</span><span class="s">'Content-Type'</span><span class="p">:</span> <span class="s">'application/json'</span><span class="p">}</span> <span class="n">WEB_HOOK_URL</span> <span class="o">=</span> <span class="s">"https://hooks.slack.com/services/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"</span> <span class="c1"># オブジェクトを組み立てる </span> <span class="n">request</span> <span class="o">=</span> <span class="n">urllib</span><span class="p">.</span><span class="n">request</span><span class="p">.</span><span class="n">Request</span><span class="p">(</span> <span class="n">WEB_HOOK_URL</span><span class="p">,</span> <span class="n">data</span><span class="o">=</span><span class="n">send_text</span><span class="p">.</span><span class="n">encode</span><span class="p">(</span><span class="s">'utf-8'</span><span class="p">),</span> <span class="n">method</span><span class="o">=</span><span class="n">method</span> <span class="p">)</span> <span class="c1"># ここで実際にPOSTが実行される </span> <span class="k">with</span> <span class="n">urllib</span><span class="p">.</span><span class="n">request</span><span class="p">.</span><span class="n">urlopen</span><span class="p">(</span><span class="n">request</span><span class="p">)</span> <span class="k">as</span> <span class="n">response</span><span class="p">:</span> <span class="n">response_body</span> <span class="o">=</span> <span class="n">response</span><span class="p">.</span><span class="n">read</span><span class="p">().</span><span class="n">decode</span><span class="p">(</span><span class="s">'utf-8'</span><span class="p">)</span> <span class="c1"># 最初にLambdaから呼ばれる </span><span class="k">def</span> <span class="nf">lambda_handler</span><span class="p">(</span><span class="n">event</span><span class="p">,</span> <span class="n">context</span><span class="p">):</span> <span class="n">response</span> <span class="o">=</span> <span class="n">post_slack</span><span class="p">()</span> <span class="k">return</span> <span class="n">response</span> |
動作確認
lambdaからテスト
を実行
- この際にテストするJSON文字列は適当で良い(コードの中で利用していないから)