├── __init__.py ├── ptb ├── __init__.py ├── requirements.txt └── ptb_lambda.py ├── tests ├── __init__.py ├── unit │ ├── __init__.py │ └── test_handler.py ├── integration │ ├── __init__.py │ └── test_api_gateway.py └── requirements.txt ├── .gitignore ├── Architecture.png ├── samconfig.toml ├── template.yaml ├── events └── event.json └── README.md /__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /ptb/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /tests/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /tests/unit/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .aws-sam/ 2 | -------------------------------------------------------------------------------- /tests/integration/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /ptb/requirements.txt: -------------------------------------------------------------------------------- 1 | python-telegram-bot==13.14 -------------------------------------------------------------------------------- /tests/requirements.txt: -------------------------------------------------------------------------------- 1 | pytest 2 | pytest-mock 3 | boto3 -------------------------------------------------------------------------------- /Architecture.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jojo786/Sample-Python-Telegram-Bot-AWS-Serverless/HEAD/Architecture.png -------------------------------------------------------------------------------- /samconfig.toml: -------------------------------------------------------------------------------- 1 | version = 0.1 2 | [default] 3 | [default.deploy] 4 | [default.deploy.parameters] 5 | stack_name = "Sample-Python-Telegram-Bot-AWS-Serverless" 6 | s3_bucket = "aws-sam-cli-managed-default-samclisourcebucket-1q7y9uu7czp2p" 7 | s3_prefix = "Sample-Python-Telegram-Bot-AWS-Serverless" 8 | region = "eu-west-1" 9 | capabilities = "CAPABILITY_IAM" 10 | disable_rollback = true 11 | image_repositories = [] 12 | -------------------------------------------------------------------------------- /template.yaml: -------------------------------------------------------------------------------- 1 | AWSTemplateFormatVersion: '2010-09-09' 2 | Transform: AWS::Serverless-2016-10-31 3 | Description: > 4 | Sample-Python-Telegram-Bot-AWS-Serverless 5 | 6 | Globals: 7 | Function: 8 | Timeout: 3 9 | Runtime: python3.12 10 | Tags: 11 | project: "Python-Telegram-Bot" 12 | Architectures: 13 | - arm64 14 | LoggingConfig: 15 | LogGroup: !Sub /aws/lambda/${AWS::StackName} 16 | LogFormat: JSON 17 | 18 | Resources: 19 | PTBFunction: 20 | Type: AWS::Serverless::Function 21 | Properties: 22 | CodeUri: ptb/ 23 | Handler: ptb_lambda.lambda_handler 24 | FunctionUrlConfig: 25 | AuthType: NONE 26 | 27 | Outputs: 28 | TelegramApi: 29 | Description: "Lambda URL for PTB function" 30 | Value: 31 | Fn::GetAtt: PTBFunctionUrl.FunctionUrl 32 | -------------------------------------------------------------------------------- /ptb/ptb_lambda.py: -------------------------------------------------------------------------------- 1 | import json 2 | from telegram.ext import Dispatcher, MessageHandler, Filters 3 | from telegram import Update, Bot 4 | 5 | bot = Bot(token="--INSERT YOUR BOT TOKEN HERE--") 6 | dispatcher = Dispatcher(bot, None, use_context=True) 7 | 8 | def echo(update, context): 9 | 10 | chat_id = update.message.chat_id 11 | chat_user = update.message.from_user 12 | chat_text = update.message.text 13 | 14 | context.bot.send_message(chat_id=chat_id, text= "Message from " + str(chat_user.first_name) + ": \n " + chat_text) 15 | 16 | def lambda_handler(event, context): 17 | 18 | dispatcher.add_handler(MessageHandler(Filters.text, echo)) 19 | 20 | try: 21 | dispatcher.process_update( 22 | Update.de_json(json.loads(event["body"]), bot) 23 | ) 24 | 25 | except Exception as e: 26 | print(e) 27 | return {"statusCode": 500} 28 | 29 | return {"statusCode": 200} 30 | 31 | -------------------------------------------------------------------------------- /tests/integration/test_api_gateway.py: -------------------------------------------------------------------------------- 1 | import os 2 | from unittest import TestCase 3 | 4 | import boto3 5 | import requests 6 | 7 | """ 8 | Make sure env variable AWS_SAM_STACK_NAME exists with the name of the stack we are going to test. 9 | """ 10 | 11 | 12 | class TestApiGateway(TestCase): 13 | api_endpoint: str 14 | 15 | @classmethod 16 | def get_stack_name(cls) -> str: 17 | stack_name = os.environ.get("AWS_SAM_STACK_NAME") 18 | if not stack_name: 19 | raise Exception( 20 | "Cannot find env var AWS_SAM_STACK_NAME. \n" 21 | "Please setup this environment variable with the stack name where we are running integration tests." 22 | ) 23 | 24 | return stack_name 25 | 26 | def setUp(self) -> None: 27 | """ 28 | Based on the provided env variable AWS_SAM_STACK_NAME, 29 | here we use cloudformation API to find out what the HelloWorldApi URL is 30 | """ 31 | stack_name = TestApiGateway.get_stack_name() 32 | 33 | client = boto3.client("cloudformation") 34 | 35 | try: 36 | response = client.describe_stacks(StackName=stack_name) 37 | except Exception as e: 38 | raise Exception( 39 | f"Cannot find stack {stack_name}. \n" f'Please make sure stack with the name "{stack_name}" exists.' 40 | ) from e 41 | 42 | stacks = response["Stacks"] 43 | 44 | stack_outputs = stacks[0]["Outputs"] 45 | api_outputs = [output for output in stack_outputs if output["OutputKey"] == "HelloWorldApi"] 46 | self.assertTrue(api_outputs, f"Cannot find output HelloWorldApi in stack {stack_name}") 47 | 48 | self.api_endpoint = api_outputs[0]["OutputValue"] 49 | 50 | def test_api_gateway(self): 51 | """ 52 | Call the API Gateway endpoint and check the response 53 | """ 54 | response = requests.get(self.api_endpoint) 55 | self.assertDictEqual(response.json(), {"message": "hello world"}) 56 | -------------------------------------------------------------------------------- /events/event.json: -------------------------------------------------------------------------------- 1 | { 2 | "body": "{\"message\": \"hello world\"}", 3 | "resource": "/hello", 4 | "path": "/hello", 5 | "httpMethod": "GET", 6 | "isBase64Encoded": false, 7 | "queryStringParameters": { 8 | "foo": "bar" 9 | }, 10 | "pathParameters": { 11 | "proxy": "/path/to/resource" 12 | }, 13 | "stageVariables": { 14 | "baz": "qux" 15 | }, 16 | "headers": { 17 | "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8", 18 | "Accept-Encoding": "gzip, deflate, sdch", 19 | "Accept-Language": "en-US,en;q=0.8", 20 | "Cache-Control": "max-age=0", 21 | "CloudFront-Forwarded-Proto": "https", 22 | "CloudFront-Is-Desktop-Viewer": "true", 23 | "CloudFront-Is-Mobile-Viewer": "false", 24 | "CloudFront-Is-SmartTV-Viewer": "false", 25 | "CloudFront-Is-Tablet-Viewer": "false", 26 | "CloudFront-Viewer-Country": "US", 27 | "Host": "1234567890.execute-api.us-east-1.amazonaws.com", 28 | "Upgrade-Insecure-Requests": "1", 29 | "User-Agent": "Custom User Agent String", 30 | "Via": "1.1 08f323deadbeefa7af34d5feb414ce27.cloudfront.net (CloudFront)", 31 | "X-Amz-Cf-Id": "cDehVQoZnx43VYQb9j2-nvCh-9z396Uhbp027Y2JvkCPNLmGJHqlaA==", 32 | "X-Forwarded-For": "127.0.0.1, 127.0.0.2", 33 | "X-Forwarded-Port": "443", 34 | "X-Forwarded-Proto": "https" 35 | }, 36 | "requestContext": { 37 | "accountId": "123456789012", 38 | "resourceId": "123456", 39 | "stage": "prod", 40 | "requestId": "c6af9ac6-7b61-11e6-9a41-93e8deadbeef", 41 | "requestTime": "09/Apr/2015:12:34:56 +0000", 42 | "requestTimeEpoch": 1428582896000, 43 | "identity": { 44 | "cognitoIdentityPoolId": null, 45 | "accountId": null, 46 | "cognitoIdentityId": null, 47 | "caller": null, 48 | "accessKey": null, 49 | "sourceIp": "127.0.0.1", 50 | "cognitoAuthenticationType": null, 51 | "cognitoAuthenticationProvider": null, 52 | "userArn": null, 53 | "userAgent": "Custom User Agent String", 54 | "user": null 55 | }, 56 | "path": "/prod/hello", 57 | "resourcePath": "/hello", 58 | "httpMethod": "POST", 59 | "apiId": "1234567890", 60 | "protocol": "HTTP/1.1" 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /tests/unit/test_handler.py: -------------------------------------------------------------------------------- 1 | import json 2 | 3 | import pytest 4 | 5 | from hello_world import app 6 | 7 | 8 | @pytest.fixture() 9 | def apigw_event(): 10 | """ Generates API GW Event""" 11 | 12 | return { 13 | "body": '{ "test": "body"}', 14 | "resource": "/{proxy+}", 15 | "requestContext": { 16 | "resourceId": "123456", 17 | "apiId": "1234567890", 18 | "resourcePath": "/{proxy+}", 19 | "httpMethod": "POST", 20 | "requestId": "c6af9ac6-7b61-11e6-9a41-93e8deadbeef", 21 | "accountId": "123456789012", 22 | "identity": { 23 | "apiKey": "", 24 | "userArn": "", 25 | "cognitoAuthenticationType": "", 26 | "caller": "", 27 | "userAgent": "Custom User Agent String", 28 | "user": "", 29 | "cognitoIdentityPoolId": "", 30 | "cognitoIdentityId": "", 31 | "cognitoAuthenticationProvider": "", 32 | "sourceIp": "127.0.0.1", 33 | "accountId": "", 34 | }, 35 | "stage": "prod", 36 | }, 37 | "queryStringParameters": {"foo": "bar"}, 38 | "headers": { 39 | "Via": "1.1 08f323deadbeefa7af34d5feb414ce27.cloudfront.net (CloudFront)", 40 | "Accept-Language": "en-US,en;q=0.8", 41 | "CloudFront-Is-Desktop-Viewer": "true", 42 | "CloudFront-Is-SmartTV-Viewer": "false", 43 | "CloudFront-Is-Mobile-Viewer": "false", 44 | "X-Forwarded-For": "127.0.0.1, 127.0.0.2", 45 | "CloudFront-Viewer-Country": "US", 46 | "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8", 47 | "Upgrade-Insecure-Requests": "1", 48 | "X-Forwarded-Port": "443", 49 | "Host": "1234567890.execute-api.us-east-1.amazonaws.com", 50 | "X-Forwarded-Proto": "https", 51 | "X-Amz-Cf-Id": "aaaaaaaaaae3VYQb9jd-nvCd-de396Uhbp027Y2JvkCPNLmGJHqlaA==", 52 | "CloudFront-Is-Tablet-Viewer": "false", 53 | "Cache-Control": "max-age=0", 54 | "User-Agent": "Custom User Agent String", 55 | "CloudFront-Forwarded-Proto": "https", 56 | "Accept-Encoding": "gzip, deflate, sdch", 57 | }, 58 | "pathParameters": {"proxy": "/examplepath"}, 59 | "httpMethod": "POST", 60 | "stageVariables": {"baz": "qux"}, 61 | "path": "/examplepath", 62 | } 63 | 64 | 65 | def test_lambda_handler(apigw_event, mocker): 66 | 67 | ret = app.lambda_handler(apigw_event, "") 68 | data = json.loads(ret["body"]) 69 | 70 | assert ret["statusCode"] == 200 71 | assert "message" in ret["body"] 72 | assert data["message"] == "hello world" 73 | # assert "location" in data.dict_keys() 74 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Sample-Python-Telegram-Bot-AWS-Serverless-PTBv13.x 2 | 3 | This is for PTB v13.x. For PTB v20.x and higher, see [this repo](https://github.com/jojo786/Sample-Python-Telegram-Bot-AWS-Serverless-PTBv20/) 4 | 5 | This project contains source code and supporting files for a [Python Telegram Bot](https://python-telegram-bot.readthedocs.io/en/stable/) v13.x serverless application, using [Webhooks](https://github.com/python-telegram-bot/python-telegram-bot/wiki/Webhooks), that you can deploy with the AWS SAM CLI. You can run this for free - the [AWS Lambda free tier](https://aws.amazon.com/lambda/pricing/) includes one million free requests per month and 400,000 GB-seconds of compute time per month. 6 | 7 | # Versions 8 | - Python 3.9 9 | - python-telegram-bot 13.14 (pinned in `requirements.txt`) 10 | 11 | # Architecture 12 | Requests come in via the [Lambda Function URL](https://aws.amazon.com/blogs/aws/announcing-aws-lambda-function-urls-built-in-https-endpoints-for-single-function-microservices/) endpoint, which get routed to a Lambda function. The Lambda function runs and posts back to Telegram. Logs are stored on CloudWatch. All of this is defined using [AWS SAM](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/what-is-sam.html), an IaC toolkit that simplifies building and running serverless applications on AWS. 13 | ![architecture](Architecture.png) 14 | 15 | It includes the following files and folders. 16 | 17 | - ptb_lambda.py - Code for the bot's Lambda function. It echos back whatever text is sent to the bot. 18 | - events - Invocation events that you can use to invoke the function. 19 | - tests - Unit tests for the application code. 20 | - template.yaml - A template that defines the application's AWS resources. 21 | - requirements.txt - which pins the version of python-telegram-bot 22 | 23 | The application uses several AWS resources, including a Lambda function and a Lambda Function URL HTTPS endpoint as a Telegram webhook. These resources are defined in the `template.yaml` file in this project. You can update the template to add AWS resources through the same deployment process that updates your application code. 24 | 25 | If you prefer to use an integrated development environment (IDE) to build and test your application, you can use the AWS Toolkit. 26 | The AWS Toolkit is an open source plug-in for popular IDEs that uses the SAM CLI to build and deploy serverless applications on AWS. The AWS Toolkit also adds a simplified step-through debugging experience for Lambda function code. See the following links to get started. 27 | 28 | * [CLion](https://docs.aws.amazon.com/toolkit-for-jetbrains/latest/userguide/welcome.html) 29 | * [GoLand](https://docs.aws.amazon.com/toolkit-for-jetbrains/latest/userguide/welcome.html) 30 | * [IntelliJ](https://docs.aws.amazon.com/toolkit-for-jetbrains/latest/userguide/welcome.html) 31 | * [WebStorm](https://docs.aws.amazon.com/toolkit-for-jetbrains/latest/userguide/welcome.html) 32 | * [Rider](https://docs.aws.amazon.com/toolkit-for-jetbrains/latest/userguide/welcome.html) 33 | * [PhpStorm](https://docs.aws.amazon.com/toolkit-for-jetbrains/latest/userguide/welcome.html) 34 | * [PyCharm](https://docs.aws.amazon.com/toolkit-for-jetbrains/latest/userguide/welcome.html) 35 | * [RubyMine](https://docs.aws.amazon.com/toolkit-for-jetbrains/latest/userguide/welcome.html) 36 | * [DataGrip](https://docs.aws.amazon.com/toolkit-for-jetbrains/latest/userguide/welcome.html) 37 | * [VS Code](https://docs.aws.amazon.com/toolkit-for-vscode/latest/userguide/welcome.html) 38 | * [Visual Studio](https://docs.aws.amazon.com/toolkit-for-visual-studio/latest/user-guide/welcome.html) 39 | 40 | ## Deploy the sample application 41 | 42 | - Create your bot using [BotFather](https://core.telegram.org/bots#6-botfather), and note the token, e.g. 12334342:ABCD124324234 43 | - Update ptb_lambda.py with the token 44 | - Install AWS CLI, and configure it 45 | 46 | The Serverless Application Model Command Line Interface (SAM CLI) is an extension of the AWS CLI that adds functionality for building and testing Lambda applications. 47 | 48 | To use the SAM CLI, you need the following tools. 49 | 50 | * SAM CLI - [Install the SAM CLI](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-install.html) 51 | * [Python 3 installed](https://www.python.org/downloads/) 52 | 53 | To build and deploy your application for the first time, run the following in your shell: 54 | 55 | ```bash 56 | sam build 57 | sam deploy --guided 58 | ``` 59 | 60 | The first command will build the source of your application. The second command will package and deploy your application to AWS, with a series of prompts: 61 | 62 | * **Stack Name**: The name of the stack to deploy to CloudFormation. This should be unique to your account and region, and a good starting point would be something matching your project name. 63 | * **AWS Region**: The AWS region you want to deploy your app to. 64 | * **Confirm changes before deploy**: If set to yes, any change sets will be shown to you before execution for manual review. If set to no, the AWS SAM CLI will automatically deploy application changes. 65 | * **Allow SAM CLI IAM role creation**: Many AWS SAM templates, including this example, create AWS IAM roles required for the AWS Lambda function(s) included to access AWS services. By default, these are scoped down to minimum required permissions. To deploy an AWS CloudFormation stack which creates or modifies IAM roles, the `CAPABILITY_IAM` value for `capabilities` must be provided. If permission isn't provided through this prompt, to deploy this example you must explicitly pass `--capabilities CAPABILITY_IAM` to the `sam deploy` command. 66 | * **Save arguments to samconfig.toml**: If set to yes, your choices will be saved to a configuration file inside the project, so that in the future you can just re-run `sam deploy` without parameters to deploy changes to your application. 67 | 68 | - You can find your Lambda Function URL Endpoint in the output values displayed after deployment. e.g. `https://1fgfgfd56.lambda-url.eu-west-1.on.aws/` 69 | - Update your [Telegram bot to change from polling to Webhook](https://xabaras.medium.com/setting-your-telegram-bot-webhook-the-easy-way-c7577b2d6f72), by pasting this URL in your browser, or curl'ing it: 70 | `https://api.telegram.org/bot12334342:ABCD124324234/setWebHook?url=https://1fgfgfd56.lambda-url.eu-west-1.on.aws/.` Use your bot token and Lambda Function URL endpoint. You can check that it was set correctly by going to `https://api.telegram.org/bot12334342:ABCD124324234/getWebhookInfo`, which should include the url of your Lambda Function URL, as well as any errors Telegram is encounterting calling your bot on that API. 71 | 72 | For future deploys, you can just run: 73 | 74 | ```bash 75 | sam build && sam deploy 76 | ``` 77 | 78 | 79 | ## Use the SAM CLI to build and test locally 80 | 81 | Build your application with the `sam build --use-container` command. 82 | 83 | ```bash 84 | Sample-Python-Telegram-Bot-AWS-Serverless$ sam build --use-container 85 | ``` 86 | 87 | The SAM CLI installs dependencies defined in `hello_world/requirements.txt`, creates a deployment package, and saves it in the `.aws-sam/build` folder. 88 | 89 | Test a single function by invoking it directly with a test event. An event is a JSON document that represents the input that the function receives from the event source. Test events are included in the `events` folder in this project. 90 | 91 | Run functions locally and invoke them with the `sam local invoke` command. 92 | 93 | ```bash 94 | Sample-Python-Telegram-Bot-AWS-Serverless$ sam local invoke HelloWorldFunction --event events/event.json 95 | ``` 96 | 97 | The SAM CLI can also emulate your application's API. Use the `sam local start-api` to run the API locally on port 3000. 98 | 99 | ```bash 100 | Sample-Python-Telegram-Bot-AWS-Serverless$ sam local start-api 101 | Sample-Python-Telegram-Bot-AWS-Serverless$ curl http://localhost:3000/ 102 | ``` 103 | 104 | 105 | ## Add a resource to your application 106 | The application template uses AWS Serverless Application Model (AWS SAM) to define application resources. AWS SAM is an extension of AWS CloudFormation with a simpler syntax for configuring common serverless application resources such as functions, triggers, and APIs. For resources not included in [the SAM specification](https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md), you can use standard [AWS CloudFormation](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-template-resource-type-ref.html) resource types. 107 | 108 | ## Fetch, tail, and filter Lambda function logs 109 | 110 | To simplify troubleshooting, SAM CLI has a command called `sam logs`. `sam logs` lets you fetch logs generated by your deployed Lambda function from the command line. In addition to printing the logs on the terminal, this command has several nifty features to help you quickly find the bug. 111 | 112 | `NOTE`: This command works for all AWS Lambda functions; not just the ones you deploy using SAM. 113 | 114 | ```bash 115 | Sample-Python-Telegram-Bot-AWS-Serverless$ sam logs -n HelloWorldFunction --stack-name Sample-Python-Telegram-Bot-AWS-Serverless --tail 116 | ``` 117 | 118 | You can find more information and examples about filtering Lambda function logs in the [SAM CLI Documentation](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-logging.html). 119 | 120 | ## Tests 121 | 122 | Tests are defined in the `tests` folder in this project. Use PIP to install the test dependencies and run tests. 123 | 124 | ```bash 125 | Sample-Python-Telegram-Bot-AWS-Serverless$ pip install -r tests/requirements.txt --user 126 | # unit test 127 | Sample-Python-Telegram-Bot-AWS-Serverless$ python -m pytest tests/unit -v 128 | # integration test, requiring deploying the stack first. 129 | # Create the env variable AWS_SAM_STACK_NAME with the name of the stack we are testing 130 | Sample-Python-Telegram-Bot-AWS-Serverless$ AWS_SAM_STACK_NAME= python -m pytest tests/integration -v 131 | ``` 132 | 133 | ## Cleanup 134 | 135 | To delete the sample application that you created, use the AWS CLI. Assuming you used your project name for the stack name, you can run the following: 136 | 137 | ```bash 138 | sam delete 139 | ``` 140 | 141 | ## Resources 142 | 143 | See the [AWS SAM developer guide](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/what-is-sam.html) for an introduction to SAM specification, the SAM CLI, and serverless application concepts. 144 | 145 | Next, you can use AWS Serverless Application Repository to deploy ready to use Apps that go beyond hello world samples and learn how authors developed their applications: [AWS Serverless Application Repository main page](https://aws.amazon.com/serverless/serverlessrepo/) 146 | 147 | # Whats Next? 148 | - Instead of storing the Telegram token in your source code, use [Lambda Environment Variables](https://docs.aws.amazon.com/lambda/latest/dg/configuration-envvars.html), or [AWS SSM](https://aws.amazon.com/blogs/compute/sharing-secrets-with-aws-lambda-using-aws-systems-manager-parameter-store/) to securely store the token 149 | - Want to store data in a serverless database? Try out [Amazon DynamoDB](https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api-dynamo-db.html) 150 | - High load, and what to respond async to Telegram? Try adding an [SQS queue in front of the Lambda function](https://serverlessland.com/patterns/sqs-lambda) 151 | - Check out [this fully featured Telegram Bot](https://github.com/jojo786/TelegramTasweerBot) running on AWS Serverless --------------------------------------------------------------------------------