質問編集履歴

2

コードの追加

2022/10/07 13:54

投稿

konan513
konan513

スコア2

test CHANGED
File without changes
test CHANGED
@@ -158,3 +158,95 @@
158
158
  もし分かる方いらっしゃいましたら回答お願いしたいです。
159
159
  よろしくお願いします。
160
160
 
161
+
162
+ ※ひとつ前のLambdaも追加させていただきます。
163
+ event変数は恐らく 2 でSQSに送信されていると思うのですが…
164
+
165
+ ```python3.9
166
+ import json
167
+ import boto3
168
+ from boto3.dynamodb.conditions import Key, Attr
169
+
170
+ # boto3からDynamoDBへアクセスするためのオブジェクトを取得
171
+ dynamodb = boto3.resource('dynamodb')
172
+
173
+ # "amazonconnect-contact-list"へアクセスするためのオブジェクトを取得
174
+ contactlist = dynamodb.Table("amazonconnect-contact-list")
175
+
176
+ # "amazonconnect-response-status"へアクセスするためのオブジェクトを取得
177
+ responsestatus = dynamodb.Table("amazonconnect-response-status")
178
+
179
+ # CallStatusを"NG"に変更する関数
180
+ def status_ng():
181
+ response = responsestatus.update_item(
182
+ Key={
183
+ 'No': 1,
184
+ 'Name': "Response"
185
+ },
186
+ UpdateExpression="set CallStatus=:c",
187
+ ExpressionAttributeValues={
188
+ ':c': "NG"
189
+ },
190
+ ReturnValues="UPDATED_NEW"
191
+ )
192
+ return response
193
+
194
+
195
+ # "amazonconnect-contact-list"の内容を返す関数
196
+ def operation_scan():
197
+
198
+ scanData = contactlist.scan()
199
+ items=scanData['Items']
200
+
201
+ return scanData["Items"]
202
+
203
+
204
+ # 指定されたプライオリティの電話番号を返す関数
205
+ def get_phone_number(json_contactinfo, now_priority):
206
+
207
+ for line in json_contactinfo:
208
+ if line['Priority']==now_priority:
209
+ phone_number = line['Phone']
210
+
211
+ return phone_number
212
+
213
+
214
+ def lambda_handler(event, context):
215
+
216
+ # 応答結果を初期化
217
+ status_ng()
218
+
219
+ # 電話番号リストを取得
220
+ contactinfo = operation_scan()
221
+
222
+ # 初回のプライオリティを"1"に設定
223
+ priority = 1
224
+
225
+ # 周回数を"2"に設定
226
+ cycle = 2
227
+
228
+ # 指定したプライオリティの電話番号を取得
229
+ phone_number = get_phone_number(contactinfo, priority)
230
+
231
+ # boto3からAmazonConnectへアクセスするためのオブジェクトを取得
232
+ connect = boto3.client('connect')
233
+
234
+ # AmzaonConnectの問い合わせフローを呼び出し電話発信
235
+ connect.start_outbound_voice_contact(
236
+ DestinationPhoneNumber=phone_number,
237
+ ContactFlowId='***',
238
+ InstanceId='***',
239
+ SourcePhoneNumber='***',
240
+ )
241
+
242
+ # boto3からSQSへアクセスするためのオブジェクトを取得
243
+ sqs = boto3.resource('sqs')
244
+
245
+ # "amazonconnect-queue-confirm"へアクセスするためのオブジェクトを取得
246
+ name = 'amazonconnect-queue-confirm'
247
+ queue = sqs.get_queue_by_name(QueueName=name)
248
+
249
+ # 現在のプライオリティと周回数をセットしてステータス確認用のキューにメッセージ送信
250
+ response = queue.send_message(MessageBody=json.dumps({"priority": priority, "cycle": cycle}))
251
+ ```
252
+

1

ご指摘ありがとうございます。体裁を整えさせていただきました。

2022/10/07 05:03

投稿

konan513
konan513

スコア2

test CHANGED
File without changes
test CHANGED
@@ -1,7 +1,10 @@
1
1
  ### 前提
2
2
 
3
- AWSで自動電話通知システムを作っています。
3
+ AWSで自動電話通知システムを作っています。(Lambda Python3.9)
4
+
5
+ https://infrastructure-engineer.com/aws-amzaonconnect-008/#index_id3
6
+
4
- Lambda(Python3.9)でコードを入力していたのですが下記のようなエラーがでてしまいました。
7
+ こちらのサイトを拝見しコードを入力していたのですが下記のようなエラーがでてしまいました。
5
8
 
6
9
 
7
10
  ### 発生している問題・エラーメッセージ
@@ -14,7 +17,7 @@
14
17
  cycle = json_record["cycle"]
15
18
 
16
19
  ### 該当のソースコード
17
-
20
+ ```python3.9
18
21
  import json
19
22
  import boto3
20
23
  from boto3.dynamodb.conditions import Key, Attr
@@ -29,6 +32,7 @@
29
32
  responsestatus = dynamodb.Table("amazonconnect-response-status")
30
33
 
31
34
  #残り周回数を取得する関数
35
+
32
36
  def get_cycle(sqs_event):
33
37
 
34
38
  record = sqs_event['Records'][0]
@@ -148,6 +152,8 @@
148
152
 
149
153
  # 現在のプライオリティと周回数をセットしてステータス確認用のキューにメッセージ送信
150
154
  response = queue.send_message(MessageBody=json.dumps({"priority": priority, "cycle": now_cycle}))
155
+ ```
156
+
151
157
 
152
158
  もし分かる方いらっしゃいましたら回答お願いしたいです。
153
159
  よろしくお願いします。