项目作者: infamily

项目描述 :
Infinity Project Telegram Bot
高级语言: Python
项目地址: git://github.com/infamily/infinity-telegram.git
创建时间: 2017-12-05T13:58:00Z
项目社区:https://github.com/infamily/infinity-telegram

开源协议:

下载


Introduction

pipenv (https://github.com/pypa/pipenv) is used instead of standard pip.

Setup development environment:
pipenv install

If you want to get native requirements.txt, use pipenv lock with -r key

pipenv lock -r > requirements.txt

Configuration

All various (and sensitive) settings can be defined via .env file.

```example .env

TELEGRAM_BOT_TOKEN=123456789:ABCDEF_1234567890123456789012345678
SENTRY_DSN=token@sentry.io/12345678"">https://token:token@sentry.io/12345678
SENTRY_LOGGING_LEVEL=40

  1. # AWS Deployment guide

NB!
Assumed that Amazon services (Amazon RDS, Amazon Lambda and Amazon SNS) will be configured in the same Amazon region

  1. * AWS cli command-line tool should be installed
  2. https://aws.amazon.com/cli/
  3. * AWS cli should be configured
  4. * IAM account for deployment should have AdministratorAccess policy OR read more about needed policies
  5. https://github.com/Miserlou/Zappa/issues/849 and https://github.com/Miserlou/Zappa/issues/244
  6. * Initialize zappa with

zappa init

  1. * Use ```zappa deploy dev``` or ```zappa deploy production``` for deploy the code.
  2. At this point Amazon Lambda function for the bot deployment will be created.
  3. * Configure Amazon RDS.
  4. Postgres guide:
  5. https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/CHAP_GettingStarted.CreatingConnecting.PostgreSQL.html
  6. When DB instance will be created, you will get actual credentials to configure
  7. `DATABASE_URL` for the django app.

NB! It is preferred to specify DATABASE_URL in the Lambda function’s environment
variables instead of .env file or django settings - because you won’t be able to
connect to Amazon RDS instance locally and locally-stored DATABASE_URL would be an obstacle.

  1. For the next step you should to configure Amazon VPC to make possible connections between
  2. `Bot <-> Database` and `Bot <-> Internet`.
  3. ## Example
  4. Please, see the following docs:
  5. https://docs.aws.amazon.com/lambda/latest/dg/vpc-rds.html
  6. Generally, if you don't have any custom configuration of VPC, steps should look like:
  7. ```You shouldn't use IP addresses or any other identifiers from the screenshots. You will have your own.

Subnets that created with RDS considered as "inernal subnets"

  • Add 3 subnets internal to existed route table (will considered as “internal route table”)
    (Subnet associations tab)

https://github.com/infamily/infinity-docs/blob/master/Example_AWS_Network/Subnets__internal_routes.png

  • Create new subnet (will considered as “outer”)

https://github.com/infamily/infinity-docs/blob/master/Example_AWS_Network/Subnets.png

  • Create another route table (“outer”) and add previous subnet to it

https://github.com/infamily/infinity-docs/blob/master/Example_AWS_Network/Route_tables.png

  • Create NAT gateway

https://github.com/infamily/infinity-docs/blob/master/Example_AWS_Network/NAT_gateway.png

  • Change routes in the “internal” route table: change target for 0.0.0.0/0 to intenet gateway

https://github.com/infamily/infinity-docs/blob/master/Example_AWS_Network/Subnets__internal_subnet_associations.png
https://github.com/infamily/infinity-docs/blob/master/Example_AWS_Network/Subnets__internal_routes.png

  • Change routes in the “outer” route table: change target for 0.0.0.0/0 to nat gateway

https://github.com/infamily/infinity-docs/blob/master/Example_AWS_Network/Subnets__outer_subnet_associations.png
https://github.com/infamily/infinity-docs/blob/master/Example_AWS_Network/Subnets__outer_routes.png

  • Add “internal subnets” to RDS security group, “Inbound” tab
    (EC2 serivce, “Security Groups” in the left sidebar)

https://github.com/infamily/infinity-docs/blob/master/Example_AWS_Network/Security_Group.png

  • Select VPC and add all subnets and all security groups to lambda vpc configuration
    (Lambda service, “Network” block in the your lambda function)

https://github.com/infamily/infinity-docs/blob/master/Example_AWS_Network/Lambda_Network.png

Example zappa_settings.json

(do not use it as is!)

  1. {
  2. "dev": {
  3. "app_function": "config.wsgi.application",
  4. "aws_region": "eu-central-1",
  5. "profile_name": "default",
  6. "project_name": "infty-telegram",
  7. "runtime": "python3.6",
  8. "s3_bucket": "zappa-89cmidgtq",
  9. "django_settings": "config.settings.dev",
  10. "events": [
  11. {
  12. "function": "lambda.notify_subscribers_about_new_topic",
  13. "event_source": {
  14. "arn": "<arn>",
  15. "events": [
  16. "sns:Publish"
  17. ]
  18. }
  19. }
  20. ]
  21. }
  22. }

Database initialize

  1. Assumed that you've configured Amazon RDS and djano database
  2. connection (in the `DATABASE_URL` environment variable, for example)

Use zappa manage <stage> migrate for apply migrations

Environment variables

NB! AWS Lambda service has own way to define environment variables.
Variables defined this way have more priority, for example:

  1. (.env file)
  2. TEST=foo
  1. (Lambda function variables)
  2. TEST=bar
  1. (interpreter)
  2. >>> os.getenv('TEST')
  3. bar

Connect to Amazon SNS

Assumed that you’ve configured Amazon SNS and have actual ARN on the API side

To configure SNS arn add the next lines in the zappa_settings.json
(INTO <stage_name> object)

Please, fill <arn> of the Amazon SNS topic

  1. "<stage_name>": {
  2. ...
  3. "events": [
  4. {
  5. "function": "lambda.notify_subscribers_about_new_topic",
  6. "event_source": {
  7. "arn": "<arn>",
  8. "events": [
  9. "sns:Publish"
  10. ]
  11. }
  12. }
  13. ]
  14. ...
  15. }

Set telegram webhook

  1. ./manage.py set_webhook htts://<lambda_url>/telegram/webhook

Note the telegram/webhook url part