Closed rashmnai closed 2 years ago
@javydekoning Do you need any other information? Please let me know.
Hi @rashmnai I tried to replicate locally but wasn't able to. However, I did push a few minor changes.
With the above as input it seems to work fine:
sam build --use-container && sam local invoke LexAdapter -e /Users/javy/github/amazon-lex-teams-adapter/events/rashmnai.json
Starting Build inside a container
Your template contains a resource with logical ID "ServerlessRestApi", which is a reserved logical ID in AWS SAM. It could result in unexpected behaviors and is not recommended.
Building codeuri: /Users/javy/github/amazon-lex-teams-adapter/app runtime: python3.8 metadata: {} architecture: x86_64 functions: LexAdapter
Fetching public.ecr.aws/sam/build-python3.8:latest-x86_64 Docker container image......
Mounting /Users/javy/github/amazon-lex-teams-adapter/app as /tmp/samcli/source:ro,delegated inside runtime container
Build Succeeded
Built Artifacts : .aws-sam/build
Built Template : .aws-sam/build/template.yaml
Commands you can use next
=========================
[*] Validate SAM template: sam validate
[*] Invoke Function: sam local invoke
[*] Test Function in the Cloud: sam sync --stack-name {stack-name} --watch
[*] Deploy: sam deploy --guided
Running PythonPipBuilder:ResolveDependencies
Running PythonPipBuilder:CopySource
Invoking lambda_function.lambda_handler (python3.8)
Skip pulling image and use local one: public.ecr.aws/sam/emulation-python3.8:rapid-1.53.0-x86_64.
Mounting /Users/javy/github/amazon-lex-teams-adapter/.aws-sam/build/LexAdapter as /var/task:ro,delegated inside runtime container
[INFO] 2022-07-06T10:47:08.413Z <<lex-teams-adapter>> Initializing boto3 clients for SSM and Lex
[INFO] 2022-07-06T10:47:08.425Z Found credentials in environment variables.
[INFO] 2022-07-06T10:47:08.597Z f4ad0467-9bca-4f0f-b481-2331d895cb20 lambda_handler: Lambda event = {"resource": "/{proxy+}", "path": "/chatops", "httpMethod": "POST", "headers": {"Authorization": "Bearer abc", "CloudFront-Forwarded-Proto": "https", "CloudFront-Is-Desktop-Viewer": "true", "CloudFront-Is-Mobile-Viewer": "false", "CloudFront-Is-SmartTV-Viewer": "false", "CloudFront-Is-Tablet-Viewer": "false", "CloudFront-Viewer-Country": "US", "Content-Type": "application/json; charset=utf-8", "Host": "abc.execute-api.us-east-1.amazonaws.com", "MS-CV": "FBNTsXTVgEarDOrxUFTi/g.1.1.1.810515605.1.2", "User-Agent": "Microsoft-SkypeBotApi (Microsoft-BotFramework/3.0)", "Via": "1.1 e943d5f0cbb0d255d29da0ddf6639ba8.cloudfront.net (CloudFront)", "X-Amz-Cf-Id": "DohlFOdjXVB6HyTFzyPXszH_x5GXmorZlgLSUVeYxWBfaAlH9VslJA==", "X-Amzn-Trace-Id": "Root=1-62bb421e-2e9d44184592f9843aa47a52", "X-Forwarded-For": "52.114.159.141, 15.158.0.116", "X-Forwarded-Port": "443", "X-Forwarded-Proto": "https", "x-ms-conversation-id": "a:1o_puwV0GFeV_x-Pq5C4VeBRA679hLqhMBxqyG8L35pjHrJLXHICSnHqHKtads4rpwztYmzsOqPZOwyS-Ij8scDm-tes0iz00IFs52q-AO66mLiUYKBPdOXMBU1kCNnll", "x-ms-tenant-id": "a0152876"}, "multiValueHeaders": {"Authorization": ["Bearer eyJhbGciOiJSUzI1NiIsImtpZCI6IkctSEd3akFRVGVFRmprRzdOY3hqZXYycEp5TSIsInR5cCI6IkpXVCIsIng1dCI6IkctSEd3akFRVGVFRmprRzdOY3hqZXYycEp5TSJ9.eyJzZX"], "CloudFront-Forwarded-Proto": ["https"], "CloudFront-Is-Desktop-Viewer": ["true"], "CloudFront-Is-Mobile-Viewer": ["false"], "CloudFront-Is-SmartTV-Viewer": ["false"], "CloudFront-Is-Tablet-Viewer": ["false"], "CloudFront-Viewer-Country": ["US"], "Content-Type": ["application/json; charset=utf-8"], "Host": ["abc.execute-api.us-east-1.amazonaws.com"], "MS-CV": ["FBNTsXTVgEarDOrxUFTi/g.1.1.1.810515605.1.2"], "User-Agent": ["Microsoft-SkypeBotApi (Microsoft-BotFramework/3.0)"], "Via": ["1.1 e943d5f0cbb0d255d29da0ddf6639ba8.cloudfront.net (CloudFront)"], "X-Amz-Cf-Id": ["DohlFOdjXVB6HyTFzyPXszH_x5GXmorZlgLSUVeYxWBfaAlH9VslJA=="], "X-Amzn-Trace-Id": ["Root=1-62bb421e-2e9d44184592f9843aa47a52"], "X-Forwarded-For": ["52.114.159.141, 15.158.0.116"], "X-Forwarded-Port": ["443"], "X-Forwarded-Proto": ["https"], "x-ms-conversation-id": ["a:1o_puwV0GFeV_x-Pq5C4VeBRA679hLqhMBxqyG8L35pjHrJLXHICSnHqHKtads4rpwztYmzsOqPZOwyS-Ij8scDm-tes0iz00IFs52q-AO66mLiUYKBPdOXMBU1kCNnll"], "x-ms-tenant-id": ["a0152876"]}, "queryStringParameters": null, "multiValueQueryStringParameters": null, "pathParameters": {"proxy": "chatops"}, "stageVariables": null, "requestContext": {"resourceId": "cyx1gd", "resourcePath": "/{proxy+}", "httpMethod": "POST", "extendedRequestId": "UcdEwEuCoAMF_yA=", "requestTime": "28/Jun/2022:18:02:06 +0000", "path": "/Prod/chatops", "accountId": "681038591680", "protocol": "HTTP/1.1", "stage": "Prod", "domainPrefix": "snvdrr6bmh", "requestTimeEpoch": 1656439326288, "requestId": "cd82a2f1-7db0-4cc7-ba2a-f3ab5b3f4a42", "identity": {"cognitoIdentityPoolId": null, "accountId": null, "cognitoIdentityId": null, "caller": null, "sourceIp": "52.114.159.141", "principalOrgId": null, "accessKey": null, "cognitoAuthenticationType": null, "cognitoAuthenticationProvider": null, "userArn": null, "userAgent": "Microsoft-SkypeBotApi (Microsoft-BotFramework/3.0)", "user": null}, "domainName": "abc.execute-api.us-east-1.amazonaws.com", "apiId": "abc"}, "body": "{\"text\":\"start\",\"textFormat\":\"plain\",\"attachments\":[{\"contentType\":\"text/html\",\"content\":\"<p>start</p>\"}],\"type\":\"message\",\"timestamp\":\"2022-06-28T18:02:05.4251287Z\",\"localTimestamp\":\"2022-06-28T12:02:05.4251287-06:00\",\"id\":\"1656439325409\",\"channelId\":\"msteams\",\"serviceUrl\":\"https://smba.trafficmanager.net/amer/\",\"from\":{\"id\":\"29:1XR-hASynrv1Jk6xccDOAFz3S_T3PeEGdly-giThqEbaFa4tWFCyLi0CwYsx61gz9dGaMByAFwJfD-5plRzXeRQ\",\"name\":\"Nair, Rashmi\",\"aadObjectId\":\"c1943c89-86b1-4ba7-b218-c2bd76109790\"},\"conversation\":{\"conversationType\":\"personal\",\"tenantId\":\"a0152876\",\"id\":\"a:1o_puwV0GFeV_x-Pq5C4VeBRA679hLqhMBxqyG8L35pjHrJLXHICSnHqHKtads4rpwztYmzsOqPZOwyS-Ij8scDm-tes0iz00IFs52q-AO66mLiUYKBPdOXMBU1kCNnll\"},\"recipient\":{\"id\":\"28:176a5ccc-dc1b-4880-b195-dcd6ec4742fe\",\"name\":\"LexAWS\"},\"entities\":[{\"locale\":\"en-US\",\"country\":\"US\",\"platform\":\"Mac\",\"timezone\":\"America/Edmonton\",\"type\":\"clientInfo\"}],\"channelData\":{\"tenant\":{\"id\":\"a0152876\"}},\"locale\":\"en-US\",\"localTimezone\":\"America/Edmonton\"}", "isBase64Encoded": false}
[INFO] 2022-07-06T10:47:08.897Z f4ad0467-9bca-4f0f-b481-2331d895cb20 lambda_handler: request.body = {
"attachments": [
{
"content": "<p>start</p>",
"contentType": "text/html"
}
],
"channelData": {
"tenant": {
"id": "a0152876"
}
},
"channelId": "msteams",
"conversation": {
"conversationType": "personal",
"id": "a:1o_puwV0GFeV_x-Pq5C4VeBRA679hLqhMBxqyG8L35pjHrJLXHICSnHqHKtads4rpwztYmzsOqPZOwyS-Ij8scDm-tes0iz00IFs52q-AO66mLiUYKBPdOXMBU1kCNnll",
"tenantId": "a0152876"
},
"entities": [
{
"country": "US",
"locale": "en-US",
"platform": "Mac",
"timezone": "America/Edmonton",
"type": "clientInfo"
}
],
"from": {
"aadObjectId": "c1943c89-86b1-4ba7-b218-c2bd76109790",
"id": "29:1XR-hASynrv1Jk6xccDOAFz3S_T3PeEGdly-giThqEbaFa4tWFCyLi0CwYsx61gz9dGaMByAFwJfD-5plRzXeRQ",
"name": "Nair, Rashmi"
},
"id": "1656439325409",
"localTimestamp": "2022-06-28T12:02:05.4251287-06:00",
"localTimezone": "America/Edmonton",
"locale": "en-US",
"recipient": {
"id": "28:176a5ccc-dc1b-4880-b195-dcd6ec4742fe",
"name": "LexAWS"
},
"serviceUrl": "https://smba.trafficmanager.net/amer/",
"text": "start",
"textFormat": "plain",
"timestamp": "2022-06-28T18:02:05.4251287Z",
"type": "message"
}
[INFO] 2022-07-06T10:47:08.897Z f4ad0467-9bca-4f0f-b481-2331d895cb20 lambda_handler: request.body.text = start
[INFO] 2022-07-06T10:47:08.897Z f4ad0467-9bca-4f0f-b481-2331d895cb20 lambda_handler: MSAppID=x****x, client_secret=x****x
[INFO] 2022-07-06T10:47:08.897Z f4ad0467-9bca-4f0f-b481-2331d895cb20 lambda_handler: valid tenant id=x***x
Obviously, it fails afterwards because I don't have your keys.
Could you redeploy from the latest main branch and let me know if you still face the above issue?
@javydekoning I downloaded the latest code and deployed it. I see below logs in Cloudwatch for lambda function
Copy
[INFO] 2022-07-06T18:11:51.529Z 734f2ae6-e78a-40c5-ae1c-11c496674cda lambda_handler: Lambda event =
{
"resource": "/",
"path": "/",
"httpMethod": "GET",
"headers": {
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
"Accept-Encoding": "gzip, deflate, br",
"Accept-Language": "en-US,en;q=0.9",
"cache-control": "max-age=0",
"CloudFront-Forwarded-Proto": "https",
"CloudFront-Is-Desktop-Viewer": "true",
"CloudFront-Is-Mobile-Viewer": "false",
"CloudFront-Is-SmartTV-Viewer": "false",
"CloudFront-Is-Tablet-Viewer": "false",
"CloudFront-Viewer-Country": "CA",
"Host": "abc.execute-api.us-east-1.amazonaws.com",
"sec-ch-ua": "\".Not/A)Brand\";v=\"99\", \"Google Chrome\";v=\"103\", \"Chromium\";v=\"103\"",
"sec-ch-ua-mobile": "?0",
"sec-ch-ua-platform": "\"macOS\"",
"sec-fetch-dest": "document",
"sec-fetch-mode": "navigate",
"sec-fetch-site": "none",
"sec-fetch-user": "?1",
"upgrade-insecure-requests": "1",
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36",
"Via": "2.0 f82d9aba2293f3d97a.cloudfront.net (CloudFront)",
"X-Amz-Cf-Id": "85WTa0b7WWJ9vuJ8CYG5u8yJWrLuuSxFKDEvlhBg==",
"X-Amzn-Trace-Id": "Root=1-62c5d61ff61a37eabaae4",
"X-Forwarded-For": "23.17.129.129, 130.176.132.189",
"X-Forwarded-Port": "443",
"X-Forwarded-Proto": "https"
},
"multiValueHeaders": {
"Accept": [
"text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9"
],
"Accept-Encoding": [
"gzip, deflate, br"
],
"Accept-Language": [
"en-US,en;q=0.9"
],
"cache-control": [
"max-age=0"
],
"CloudFront-Forwarded-Proto": [
"https"
],
"CloudFront-Is-Desktop-Viewer": [
"true"
],
"CloudFront-Is-Mobile-Viewer": [
"false"
],
"CloudFront-Is-SmartTV-Viewer": [
"false"
],
"CloudFront-Is-Tablet-Viewer": [
"false"
],
"CloudFront-Viewer-Country": [
"CA"
],
"Host": [
"abc.execute-api.us-east-1.amazonaws.com"
],
"sec-ch-ua": [
"\".Not/A)Brand\";v=\"99\", \"Google Chrome\";v=\"103\", \"Chromium\";v=\"103\""
],
"sec-ch-ua-mobile": [
"?0"
],
"sec-ch-ua-platform": [
"\"macOS\""
],
"sec-fetch-dest": [
"document"
],
"sec-fetch-mode": [
"navigate"
],
"sec-fetch-site": [
"none"
],
"sec-fetch-user": [
"?1"
],
"upgrade-insecure-requests": [
"1"
],
"User-Agent": [
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36"
],
"Via": [
"2.0 f82d9aba0f0a6144671e362293f3d97a.cloudfront.net (CloudFront)"
],
"X-Amz-Cf-Id": [
"85WTa0bD0mk9_u8yJWrLuuSxFKDEvlhBg=="
],
"X-Amzn-Trace-Id": [
"Root=1-62c561ff61a37eabaae4"
],
"X-Forwarded-For": [
"23.17.129.129, 130.176.132.189"
],
"X-Forwarded-Port": [
"443"
],
"X-Forwarded-Proto": [
"https"
]
},
"queryStringParameters": null,
"multiValueQueryStringParameters": null,
"pathParameters": null,
"stageVariables": null,
"requestContext": {
"resourceId": "9ko1j8qzq5",
"resourcePath": "/",
"httpMethod": "GET",
"extendedRequestId": "U22ANEN9IAMFe-w=",
"requestTime": "06/Jul/2022:18:11:51 +0000",
"path": "/Stage",
"accountId": "12345",
"protocol": "HTTP/1.1",
"stage": "Stage",
"domainPrefix": "abc",
"requestTimeEpoch": 1657131111513,
"requestId": "63d206d3-9872-4d45-be3b-97bea23dea5e",
"identity": {
"cognitoIdentityPoolId": null,
"accountId": null,
"cognitoIdentityId": null,
"caller": null,
"sourceIp": "23.17.129.129",
"principalOrgId": null,
"accessKey": null,
"cognitoAuthenticationType": null,
"cognitoAuthenticationProvider": null,
"userArn": null,
"userAgent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36",
"user": null
},
"domainName": "abc.execute-api.us-east-1.amazonaws.com",
"apiId": "abc"
},
"body": null,
"isBase64Encoded": false
}
I am not sure if this is something to do with how I pass the command on Lex Bot on Teams. I am going to my Lex bot and typing "start DEVRS 10". In the logs, I saw the body as null as you see in above request body and I saw below error:
2022-07-06T10:37:55.106-06:00Copy[ERROR] TypeError: the JSON object must be str, bytes or bytearray, not NoneTypeTraceback (most recent call last): File "/var/task/lambda_function.py", line 41, in lambda_handler adapter = LexTeamsAdapter.from_json(config, event['body']) File "/var/task/lib/lex_teams_adapter.py", line 44, in from_json object = json.loads(json_string) File "/var/lang/lib/python3.8/json/__init__.py", line 341, in loads raise TypeError(f'the JSON object must be str, bytes or bytearray, ' | [ERROR] TypeError: the JSON object must be str, bytes or bytearray, not NoneType Traceback (most recent call last): File "/var/task/lambda_function.py", line 41, in lambda_handler adapter = LexTeamsAdapter.from_json(config, event['body']) File "/var/task/lib/lex_teams_adapter.py", line 44, in from_json object = json.loads(json_string) File "/var/lang/lib/python3.8/json/__init__.py", line 341, in loads raise TypeError(f'the JSON object must be str, bytes or bytearray, '
-- | --
Are you sure you have used the correct "API Gateway endpoint URL for LexAdapter" in step 4? I'm expecting a POST on path /chatops
, your log shows a GET on /
.
To be honest it's difficult for me to debug as I'm not actively using Teams anymore. I build this just for a demo I did a year ago :-)
@javydekoning I have removed chatops as I set up method for root / resource. Even with /chatops I got same error. Is it possible to get on a quick call?
@javydekoning I got this working. It looks like request body was null because of the minor changes that was pushed for app/lib/lex_teams_adapter.py. I reverted that and I was able to get it working. Thanks for all your help. I see the response in Lex Lambda cloudwatch logs was just wondering if you guys were able to post the response back to teams.
Thats great to hear, please open a pull request with the fix :-)
@javydekoning It seems I do not have access to create a new branch and create a PR, can you please provide access?
You need to fork, then open pr :-)
@javydekoning I see this part in lambda code. Is this for response back to teams? I do not see the response in teams but I do see below logs in cloudwatch:
2022-07-11T11:47:22.929-06:00Copy[INFO] 2022-07-11T17:47:22.929Z 5ed87712-fd83-4ed9-8dd6-ca39b9f4e65f **postResponseToTeam**s: message = {'type': 'message', 'from': ({'id': '28:176a5c95-dcd6ec4742fe', 'name': 'Lex},), 'conversation': {'isGroup': True, 'conversationType': 'channel', 'tenantId': 'a0179ab93', 'id': '19:gCzRm06fA1@thread.tacv2;messageid=1657551559094'}, 'recipient': {'isGroup': True, 'conversationType': 'channel', 'tenantId': 'a01593', 'id': '19:gCzRndm06fA1@thread.tacv2;messageid=1657551559094'}, 'text': 'Done', 'replyToId': '1657561640752'}
def **postResponseToTeams**(message, returnUrl, msAppId, msAppPass):
logger.info('postResponseToTeams: message = %s', message)
logger.debug('postResponseToTeams: returnUrl = %s', returnUrl)
# obtain authorization token
hed = {"Host": "login.microsoftonline.com",
"Content-Type": "application/x-www-form-urlencoded"}
authUrl = "https://login.microsoftonline.com/botframework.com/oauth2/v2.0/token"
authMessage = "grant_type=client_credentials&client_id=" + msAppId + "&client_secret=" + \
urllib.parse.quote(msAppPass, safe="") + \
"&scope=https%3A%2F%2Fapi.botframework.com%2F.default"
logger.debug(
'postResponseToTeams: obtaining MS Bot Framework access token')
r = requests.post(authUrl, data=authMessage, headers=hed)
logger.debug(
' postResponseToTeams: MS Bot Framework POST status = ' + str(r.status_code))
authReply = json.loads(r.text)
logger.debug(
'postResponseToTeams: MS Bot Framework access token = ' + json.dumps(authReply))
hed = {"Authorization": "Bearer " +
authReply["access_token"], "Content-Type": "application/json"}
r = requests.post(returnUrl, data=json.dumps(message), headers=hed)
logger.info(
'postResponseToTeams: MS Bot Framework POST status = ' + str(r.status_code))
return r.status_code
@javydekoning Okay I do see this in logs which means it tried to send response to teams but got 400:
[INFO] 2022-07-11T17:47:23.528Z 5ed87712-fd83-4ed9-8dd6-ca39b9f4e65f postResponseToTeams: MS Bot Framework POST status = 400 | [INFO] 2022-07-11T17:47:23.528Z 5ed87712-fd83-4ed9-8dd6-ca39b9f4e65f postResponseToTeams: MS Bot Framework POST status = 400
-- | --
You need to fork, then open pr :-)
I will do this after response to teams issue is fixed.
All issues are fixed and I have created a PR. Thanks for all your help.
Closing issue
@javydekoning We have followed the steps and created the bot in MS Teams and when we send a message on bot, we see below error on Lambda cloudwatch logs. Is there any additional step that has to be done ?
[ERROR] KeyError: 'name' Traceback (most recent call last): File "/var/task/lambda_function.py", line 39, in lambda_handler adapter = LexTeamsAdapter.from_json(config, event['body']) File "/opt/python/app/lib/lex_teams_adapter.py", line 48, in from_json object["from"]["name"].split(" ")[0],
Request body: