问题描述
您好,我正在尝试格式化消息以向我的空闲频道发送警卫值班警报。但是无法解析数据以将其打印出来。这是我的代码 当前错误涉及 ['accountID'] 不存在。
错误:
Response
{
"errorMessage": "'accountId'","errorType": "KeyError","stackTrace": [
" File \"/var/task/lambda_function.py\",line 37,in lambda_handler\n 'value': event['accountId'],\n"
]
}
代码:
import JSON
import requests
webhook = 'https://hooks.slack.com/services/YOUR/KEY'
def send_to_slack(event):
# Using POST from request library messgae using Webhook from
# Slack API which dumps event from JSON to Python String
# This line allows us to post new messages to your slack channel
# everytime logs are collected. It prepares and indents the messages
# as a string
try:
r = requests.post(webhook,json.dumps(event,default=str,indent=4),headers={'Content-type': 'application/json'})
r.raise_for_status()
except requests.exception.ConnectionError as errc:
raise Exception(errc)
except requests.exceptions.HTTPError as errh:
raise Exception(errh)
except requests.exception.Timeout as errt:
raise Exception(errt)
except requests.exception.RequestException as err:
raise Exception(err)
def lambda_handler(event,context):
# Print logs from CloudWatch
print(json.dumps(event))
data = {
'attachments' : [
{
'fields': [
{
'title': 'Account ID','value': event['detail']['accountId'],'short': True
}
]
}
]
}
send_to_slack(data)
return {
'statusCode': 200,'body': json.dumps('Hello from Lambda!')
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)