Skip to content
This repository has been archived by the owner on Oct 8, 2018. It is now read-only.

IgorMishchuk/chyatik

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

23 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Below is the old "meh" way. For new, shiny way check new repo.


Serverless Telegram bot

Intro

This serverless telegram bot was written as a part of self education during AWS Solutions Architect Assoiciate and AWS Developer Associate training courses.

It uses next AWS services:

  1. API Gateway;
  2. Lambda (node.js 6.10);
  3. S3;
  4. Polly;
  5. Cloudwatch;
  6. DynamoDB.

At the moment, it has three functions:

  1. Send received text to Polly and have it converted to speech. (speech.js)
  2. Calculate time difference between specific message and latest entry in DB (dynamo.js)
  3. Process text not meant for conversion to speech. (texting.js)

Prerequisites

What you need for this to work:

  1. Telegram bot. How to create Telegram bot.
  2. AWS account. Free tier is OK, but make sure that your AWS usage stays in Free tier limits.

Deployment

  1. Create S3 bucket which will be used by your Lambda function. Bucket name will be specified as Value for BUCKET_NAME environmental variable.
  2. Create DynamoDB table:
    • Table name will be specified as Value for DB environmental variable;
    • Primary key will contain name of person for whom we are calculating time difference. Key name will be used in Query and Put operations. In this example key name is "Dname". Set type to String;
    • Sort key will contain date of message from target person. In this example key name is "Date". Set type to String;
    • Untick "Use default settings";
    • Untick Autoscaling for Read and Write capacity units;
    • Set provisioned capacity to 5 for Read and Write capacity units. If you need more, please, check this guide for provisioned throughput calculation. Remember that DynamoDB is subject to free tier for 25 RCU/WCU.
  3. Create Lambda function:
    • Runtime: Node.js 6.10;
    • Role should allow:
      • Read\Write permissions to S3;
      • Full access to Polly;
      • Access to Cloudwatch logs;
      • Permissions for Query and PutItem for DynamoDB;
    • Import four files\ZIP archive to Lambda:
      • index.js
      • speech.js
      • texting.js
      • dynamo.js
    • Specify Environment variables for BOT_API_KEY, BUCKET_NAME, DB and optionally CHAT1, TEST_CHAT, USER1 and USER2. Last 4 are used in index.js to filter which chats\users are allowed to interact with specific function parts;
    • Save created Lambda function.
  4. Create API Gateway:
    • Actions -> Create method POST;
    • Integration type "Lambda Function";
    • Specify your Lambda name in "Lambda fucntion" field;
    • Actions -> Deploy API:
      • Deployment stage [New stage];
      • Stage name "main", for example.
    • Invoke URL of the stage will be used for Webhook creation to Telegram bot API. It looks like https://<API_NUMBER>.execute-api.<AWS_REGION>.amazonaws.com/<STAGE_NAME>.
  5. Create a webhook from Telegram URL to Invoke URL of API gateway. Webhook how-to. Command will look like this "curl --data url=https://<INVOKE_URL> https://api.telegram.org/bot<BOT_API_KEY>/setWebhook"
  6. Configure Bot to inline mode.

That's it, you're ready.


To clarify - I'm not a developer and code you're going to see might violate some (or many) best practices for code writing. If you do notice mistakes, please, suggest your correction with brief explanation.

Thanks.

About

Serverless Telegram bot

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published