• Stars
    star
    123
  • Rank 290,145 (Top 6 %)
  • Language
    JavaScript
  • Created over 8 years ago
  • Updated 6 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Telegram Bot using AWS API Gateway and AWS Lambda

Telegram Bot

A tutorial on creating a Node.js Telegram bot using AWS Lambda with AWS API Gateway.

Telegram Bot

Build Status

Build Status

Setup

I am choosing Asia Pacific (Singapore) region for AWS Lambda and Asia Pacific (Singapore) region for AWS API Gateway.

Telegram

  1. Go to Telegram Web.
  2. Start a chat with @BotFather.
  3. Type "/start".
  4. Type "/newbot" to create a new bot. I named my bot "lesterchan_bot".
  5. Note the HTTP API access token that @BotFather will reply you after you created the bot.

Checkout Code

$ git clone https://github.com/lesterchan/telegram-bot.git  
$ cd telegram-bot  
$ npm install --production
$ cp token.sample.js token.js

Open up token.js and fill in your Telegram HTTP API access token obtained in the first step then run this command:

$ zip -r telegram-bot.zip *.js node_modules/*

AWS Lambda

  1. Go to AWS Lambda.
  2. Click "Get Started Now".
  3. Under the "Select blueprint" screen, search for "hello-world"and you will see the hello-world blueprint which says "A starter AWS Lambda function.".
  4. Click on "hello-world" (NOT "hello-world-python").
  5. You will be brought to the "Configure Function" page.
  6. Under "Name", you can choose any name for your function. I called it "telegram-bot".
  7. Under "Runtime", ensure it is "Node.js".
  8. Under "Code entry type", choose "Upload a .ZIP file" and click the "Upload" button" to browse for the file "telegram-bot.zip" which you have zipped previously.
  9. Under "Handler", we leave it as "index.handler".
  10. Under "Role", we choose "Basic Execution Role".
  11. You will be brought to a "Role Summary" page.
  12. Under "IAM Role", choose "lambda_basic_execution".
  13. Under "Role Name", choose "oneClick_lambda_basic_execution_.....".
  14. Click "Allow".
  15. You will be brought back to the "Configure Function" page.
  16. Leave "Memory (MB)" as "128MB".
  17. You might want to increase "Timeout" to "15" seconds.
  18. Under VPC, choose "No VPC".
  19. Click "Next".
  20. Click "Create function".

AWS API Gateway

  1. Go to AWS API Gateway.
  2. Click "Get Started Now".
  3. Under "API name", enter the name of your API. I will just name it "Telegram Bot".
  4. Click "Create API".
  5. You will be redirected to the "Resources" page.
  6. Click "Create Method" and on the dropdown menu on the left, choose "POST" and click on the "tick" icon.
  7. Now, you will see the "/ - POST - Setup" page on the right.
  8. Under "Integration Type", choose "Lambda Function".
  9. Under "Lambda Region", choose "ap-southeast-1".
  10. Under "Lambda Function", type "telegram" and it should auto-complete it to "telegram-bot".
  11. Click "Save" and "Ok" when the popup appears.
  12. You will be brought to the "/ - POST - Method Execution" Page.
  13. Click "Integration Request".
  14. Click "Mapping Templates" and the section should expand.
  15. Click "Add Mapping Template" and type in "application/json" and click on the "tick" icon.
  16. Under "Input Passthrough" on the right, click on the "pencil" icon.
  17. Choose "Mapping Template" on the dropdown that appears.
  18. Copy and paste {"body": $input.json('$')} to the template box.
  19. Click on the "tick" icon beside the dropdown once you are done.
  20. Click on "Deploy API" button on the top left.
  21. Under "Deployment Stage", click "New Stage".
  22. Under "Stage Name", I will type in "production".
  23. Click "Deploy".
  24. Note the "Invoke URL" at the top and your API is now live.

Set Telegram Webhook

  1. Replace <ACCESS_TOKEN> with your Telegram HTTP API access token obtained in the first step.
  2. Replace <INVOKE_URL> with your Invoke URL obtained in the previous step.
  3. Run this command:
$ curl --data "url=<INVOKE_URL>" "https://api.telegram.org/bot<ACCESS_TOKEN>/setWebhook"

You should get back a response similar to this:

$ {"ok":true,"result":true,"description":"Webhook was set"}

Testing via Telegram

  1. Message your Telegram Bot that you have created.
  2. Type in "/haze" (without the quotes).
  3. You should get back a nicely formatting response as shown in the first screenshot.

Commands

Singapore Bus Arrival Timings

Usage: /bus <busStopNo> <busNo>
Example: /bus 30111 991

Usage: /bus <busStopNo>
Example: /bus 44591

Singapore Haze Situation

Usage: /haze
Example: /haze

Singapore Weather 2 Hour Forecast

Usage: /weather
Example: /weather

IP Information

Usage: /ipinfo <ip>
Example: /ipinfo 8.8.8.8

Social Stats Count For Links

Usage: /socialstats <url>
Example: /socialstats https://lesterchan.net

See Also

Slack Bot using AWS API Gateway and AWS Lamda

More Repositories

1

wp-sweep

WP-Sweep allows you to clean up unused, orphaned and duplicated data in your WordPress. It also optimizes your database tables.
PHP
153
star
2

wp-pagenavi

Adds a more advanced paging navigation interface to your WordPress blog.
PHP
133
star
3

wp-postratings

Adds an AJAX rating system for your WordPress blog's post/page.
PHP
123
star
4

wp-postviews

Enables you to display how many times a post/page had been viewed.
PHP
114
star
5

wp-polls

Adds an AJAX poll system to your WordPress blog. You can also easily add a poll into your WordPress's blog post/page.
PHP
93
star
6

wp-dbmanager

Allows you to optimize database, repair database, backup database, restore database, delete backup database , drop/empty tables and run selected queries. Supports automatic scheduling of backing up, optimizing and repairing of database.
PHP
71
star
7

slack-bot

Slack Bot using AWS API Gateway and AWS Lambda
JavaScript
56
star
8

wp-useronline

Enable you to display how many users are online on your WordPress blog with detailed statistics.
PHP
36
star
9

linkedin-pdf-resume-parser

Parse LinkedIn PDF Resume and extract out name, email, education and work experiences.
PHP
27
star
10

wp-showhide

Allows you to embed content within your blog post via WordPress ShortCode API and toggling the visibility of the cotent via a link.
PHP
24
star
11

rendeer

Puppeteer x Prerender
JavaScript
24
star
12

gamerz-file-explorer

Enables you to browse and search for folders/files on the web just like Windows Explorer.
PHP
23
star
13

wp-ban

Ban users by IP, IP Range, host name, user agent and referrer url from visiting your WordPress's blog.
PHP
22
star
14

wp-print

Displays a printable version of your WordPress blog's post/page.
PHP
21
star
15

wp-serverinfo

Display your host's PHP, MYSQL & memcached (if installed) information on your WordPress dashboard.
PHP
17
star
16

wp-downloadmanager

Adds a simple download manager to your WordPress blog.
PHP
17
star
17

wp-email

Allows people to recommend/send your WordPress blog's post/page to a friend.
PHP
14
star
18

wp-instantarticles

WP-InstantArticles generates a RSS feed of your WordPress posts as Instant Articles for Facebook to consume.
PHP
10
star
19

wp-draftsforfriends

Now you don't need to add friends as users to the blog in order to let them preview your drafts
PHP
10
star
20

products-stock-checker

Just a personal project to check products price and stock availability on Razer Online Stores and Lazada.
JavaScript
9
star
21

wp-relativedate

Displays relative date alongside with your post/comments actual date. Like 'Today', 'Yesterday', '2 Days Ago', '2 Weeks Ago', '2 'Seconds Ago', '2 Minutes Ago', '2 Hours Ago'.
PHP
6
star
22

wp-stats

Display your WordPress blog statistics. Ranging from general total statistics, some of my plugins statistics and top 10 statistics.
PHP
5
star
23

wp-commentnavi

Adds a more advanced paging navigation for your comments to your WordPress 2.8 and above blog.
PHP
4
star
24

wp-sticky

[DEPRECATED] Adds a sticky post feature to your WordPress's blog.
PHP
3
star
25

wp-pluginsused

Display WordPress plugins that you currently have (both active and inactive) onto a post/page.
PHP
3
star
26

paw-base64urlencode

A Paw Extension that allows you to generate a websafe url from a base64 encoding. Used mainly for GrabPay API.
JavaScript
2
star
27

lesterchan

Lester Chan's GitHub Profile
2
star
28

smartthings

Lester Chan's SmartThings Apps & Custom Device Handlers
Groovy
2
star
29

paw-rsa

A Paw Extension that allows you to encrypt/decrypt using RSA Public/Private Key.
JavaScript
2
star
30

gp-php-sdk-sample

GP PHP SDK Sample
PHP
1
star
31

paw-popsignature

A Paw Extension that allows you to generate a X-GID-AUX-POP HMAC signature. Used mainly for GrabPay API.
JavaScript
1
star
32

wp-wap

[DEPRECATED] Browse your WordPress's blog entries on a WAP enabled mobile phone.
PHP
1
star
33

wp-migme

Share a post to migme's Miniblog whenever you publish a post in WordPress.
PHP
1
star
34

freemyinternet

Automatically places the FreeMyInternet banner from FreeMyInternet.com on your WordPress website. The blogging community will be organising a protest and online black out on Thursday 6th June 2013, 0001 to 6th June 2013, 2359 against the new licensing by the Media Development Authority of Singapore.
PHP
1
star