{"id":20113543,"url":"https://github.com/cloudacademy/aws-lexv2-chatbot","last_synced_at":"2025-05-06T12:30:35.759Z","repository":{"id":77791347,"uuid":"418701923","full_name":"cloudacademy/aws-lexv2-chatbot","owner":"cloudacademy","description":"AWS Lex v2 Chatbot","archived":false,"fork":false,"pushed_at":"2022-05-13T06:19:59.000Z","size":13443,"stargazers_count":6,"open_issues_count":0,"forks_count":2,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-04-09T12:21:50.484Z","etag":null,"topics":["aws","chatbot","cloudacademy","devops","lex","tmdb"],"latest_commit_sha":null,"homepage":"","language":"Shell","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/cloudacademy.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-10-18T23:31:38.000Z","updated_at":"2024-09-11T06:04:12.000Z","dependencies_parsed_at":"2023-03-12T02:04:44.317Z","dependency_job_id":null,"html_url":"https://github.com/cloudacademy/aws-lexv2-chatbot","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cloudacademy%2Faws-lexv2-chatbot","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cloudacademy%2Faws-lexv2-chatbot/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cloudacademy%2Faws-lexv2-chatbot/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cloudacademy%2Faws-lexv2-chatbot/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/cloudacademy","download_url":"https://codeload.github.com/cloudacademy/aws-lexv2-chatbot/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252683373,"owners_count":21788026,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["aws","chatbot","cloudacademy","devops","lex","tmdb"],"created_at":"2024-11-13T18:24:49.239Z","updated_at":"2025-05-06T12:30:35.753Z","avatar_url":"https://github.com/cloudacademy.png","language":"Shell","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Amazon Lex Movie Recommendations Chatbot\n\n## Introduction\nA simple [Amazon Lex](https://aws.amazon.com/lex/) Chatbot that is able to provide movie recommendations for a given movie genre. This implementation is performed using [Amazon Lex V2](https://docs.aws.amazon.com/lexv2/latest/dg/what-is.html).\n\n## Amazon Lex V2 Slides\n[AWSLexV2Chatbots.pdf](./doc/AWSLexV2Chatbots.pdf)\n\n## Integration\nMovie recommendation data is provided by The Movie Database ([TMDB](https://www.themoviedb.org/)). \n\n![import chatbot package](./doc/images/TMDB.png)\n\n## Lambda Function\nThe chatbot integrates with [TMDB API](https://developers.themoviedb.org/3) endpoint using a custom AWS [Lambda Function](./lambda/lambda_function.py) developed in Python3:\n\n```python\nimport os\nfrom tmdbv3api import TMDb, Discover\n\ntmdb = TMDb()\ntmdb.api_key = os.getenv('APIKEY')\n\nGENRES = {\n\"action\": 28,\n\"adventure\": 12,\n\"animation\": 16,\n\"comedy\": 35,\n\"crime\": 80,\n\"documentary\": 99,\n\"drama\": 18,\n\"family\": 10751,\n\"Fantasy\": 14,\n\"history\": 36,\n\"horror\": 27,\n\"music\": 10402,\n\"mystery\": 9648,\n\"romance\": 10749,\n\"science fiction\": 878,\n\"thriller\": 53,\n\"war\": 10752,\n\"western\": 37\n}\n\ndef get_slots(intent_request):\n    return intent_request['sessionState']['intent']['slots']\n    \ndef get_slot(intent_request, slotName):\n    slots = get_slots(intent_request)\n    if slots is not None and slotName in slots and slots[slotName] is not None:\n        return slots[slotName]['value']['interpretedValue']\n    else:\n        return None    \n\ndef get_session_attributes(intent_request):\n    sessionState = intent_request['sessionState']\n    if 'sessionAttributes' in sessionState:\n        return sessionState['sessionAttributes']\n\n    return {}\n\ndef elicit_intent(intent_request, session_attributes, message):\n    return {\n        'sessionState': {\n            'dialogAction': {\n                'type': 'ElicitIntent'\n            },\n            'sessionAttributes': session_attributes\n        },\n        'messages': [ message ] if message != None else None,\n        'requestAttributes': intent_request['requestAttributes'] if 'requestAttributes' in intent_request else None\n    }\n\ndef close(intent_request, session_attributes, fulfillment_state, message):\n    intent_request['sessionState']['intent']['state'] = fulfillment_state\n    return {\n        'sessionState': {\n            'sessionAttributes': session_attributes,\n            'dialogAction': {\n                'type': 'Close'\n            },\n            'intent': intent_request['sessionState']['intent']\n        },\n        'messages': [message],\n        'sessionId': intent_request['sessionId'],\n        'requestAttributes': intent_request['requestAttributes'] if 'requestAttributes' in intent_request else None\n    }\n\ndef MovieRecommendation(intent_request):\n    session_attributes = get_session_attributes(intent_request)\n    slots = get_slots(intent_request)\n    genre = get_slot(intent_request, 'genre')\n\n    discover = Discover()\n    movies = discover.discover_movies({\n        'primary_release_date.gte': '2019-01-01',\n        'primary_release_date.lte': '2021-12-01',\n        'with_genres': GENRES.get(genre.lower())\n    })\n\n    recs = \"\"\n    for movie in movies:\n        recs = recs + f\" {movie.title}, \"\n\n    text = f\"Thank you. Your recommended movies are: {recs}\"\n    message =  {\n            'contentType': 'PlainText',\n            'content': text\n        }\n\n    fulfillment_state = \"Fulfilled\"    \n    return close(intent_request, session_attributes, fulfillment_state, message)\n    \ndef dispatch(intent_request):\n    intent_name = intent_request['sessionState']['intent']['name']\n    response = None\n\n    if intent_name == 'MovieRecommendation':\n        return MovieRecommendation(intent_request)\n    #elif intent_name == 'OtherIntent':\n    #    return OtherIntent(intent_request)\n\n    raise Exception('Intent with name ' + intent_name + ' not supported')\n\ndef lambda_handler(event, context):\n    response = dispatch(event)\n    return response\n```\n\n## Installation\n1. Register for a TMDB API key\n2. Within the **install.sh** file, update the ```APIKEY``` variable:\n\n**install.sh**:\n```\nAPIKEY=TMDB_API_KEY_HERE\n```\n3. Execute the **install.sh** script\n4. Log in to the AWS Lex console and then import the newly created **lex-movierecommendations.zip** package\n\n![import chatbot package](./doc/images/image1.png)\n![import chatbot package](./doc/images/image2.png)\n\nSet the following options:\n\n- **Bot name**, enter ```MovieRecommendations```\n\n- **IAM permissions**, select **create a role with basic Amazon Lex permissions**\n\n- **Childrens Online Privacy Protection Act**, select **No**\n\n5. After the import has succeeded - in the lefthand menu navigate to **Deployment/Aliases** for the newly created **MovieRecommendations** bot and click on the **TestBotAlias**\n\n![bind lambda function](./doc/images/image3.png)\n\n6. Within the **TestBotAlias** click on the **English** language\n\n![bind lambda function](./doc/images/image4.png)\n\n7. Select **movierecommendations** for **Source**, and **$LATEST** for **Lambda function version or alias**. Click Save.\n\n![bind lambda function](./doc/images/image5.png)\n\n8. In the lefthand menu navigate to **Bot versions/Draft version**, and then under **Languages** click on the **English** language link.\n\n![bind lambda function](./doc/images/image6.png)\n\n9. In the bottom menu bar click on the **build** button to compile the MovieRecommendations Chatbot.\n\n![bind lambda function](./doc/images/image7.png)\n\n10. Confirm that the MovieRecommendations Chatbot build has completed successfully.\n\n![bind lambda function](./doc/images/image8.png)\n\n11. Confirm that the MovieRecommendations Chatbot build has completed successfully, and then click on the **Test** button to activate the Chatbot.\n\n12. Within the Chatbot test pane, enter any of the following utterances to start the Chatbot conversation:\n\n```\nrecommend a movie\nrecommend a {genre} movie\nrecommend a {genre} film\nI want to watch a movie\nI want to watch a {genre} movie\n```\n\nNote: The following movie genres are supported:\n\n```\nAction\nAdventure\nAnimation\nComedy\nCrime\nDocumentary\nDrama\nFamily\nFantasy\nHistory\nHorror\nMusic\nMystery\nRomance\nScience Fiction\nThriller\nWar\nWestern\n```\n\nExample Chatbot conversation:\n\n![bind lambda function](./doc/images/image9.png)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcloudacademy%2Faws-lexv2-chatbot","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcloudacademy%2Faws-lexv2-chatbot","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcloudacademy%2Faws-lexv2-chatbot/lists"}