mirror of https://github.com/tucnak/telebot
examples: remove directory for now
parent
12d72970f3
commit
5df77562df
@ -1,23 +0,0 @@
|
||||
This example shows how to write an echo telebot for AWS Lambda and how to launch it using Terraform.
|
||||
|
||||
This bot is different from a typical bot in two ways:
|
||||
|
||||
1. It is configured with `Settings.Synchronous = true`. This disables asynchronous handlers to let Lambda wait for their completion:
|
||||
|
||||
```go
|
||||
b, _ := tele.NewBot(tele.Settings{Token: token, Synchronous: true})
|
||||
```
|
||||
|
||||
2. Instead of `Settings.Poller` and `bot.Start` it calls `bot.ProcessUpdate` inside `lambda.Start`:
|
||||
|
||||
```go
|
||||
lambda.Start(func(req events.APIGatewayProxyRequest) (err error) {
|
||||
var u tele.Update
|
||||
if err = json.Unmarshal([]byte(req.Body), &u); err == nil {
|
||||
b.ProcessUpdate(u)
|
||||
}
|
||||
return
|
||||
})
|
||||
```
|
||||
|
||||
To launch the bot [install Terraform](https://www.terraform.io/downloads.html), run [`./init.sh`](init.sh) and then [`./deploy.sh`](deploy.sh). To tear down the cloud infrastructure run `terraform destroy`.
|
@ -1,5 +0,0 @@
|
||||
#!/bin/sh
|
||||
set -ex
|
||||
cd "$(dirname "$0")"
|
||||
CGO_ENABLED=0 go build -ldflags="-s -w" -v
|
||||
terraform apply "$@"
|
@ -1,8 +0,0 @@
|
||||
module github.com/tucnak/telebot/examples/awslambdaechobot
|
||||
|
||||
go 1.14
|
||||
|
||||
require (
|
||||
github.com/aws/aws-lambda-go v1.17.0
|
||||
gopkg.in/tucnak/telebot.v2 v2.3.1
|
||||
)
|
@ -1,23 +0,0 @@
|
||||
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
|
||||
github.com/aws/aws-lambda-go v1.17.0 h1:Ogihmi8BnpmCNktKAGpNwSiILNNING1MiosnKUfU8m0=
|
||||
github.com/aws/aws-lambda-go v1.17.0/go.mod h1:FEwgPLE6+8wcGBTe5cJN3JWurd1Ztm9zN4jsXsjzKKw=
|
||||
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
|
||||
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
|
||||
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
|
||||
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
|
||||
github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4=
|
||||
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
|
||||
github.com/urfave/cli/v2 v2.1.1/go.mod h1:SE9GqnLQmjVa0iPEY0f1w3ygNIYcIJ0OKPMoW2caLfQ=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/tucnak/telebot.v2 v2.3.1 h1:ux26aeidIT1RcqOiTjZtNEdAgXoohc/CKM6imBSkzbw=
|
||||
gopkg.in/tucnak/telebot.v2 v2.3.1/go.mod h1:t+KVAiqFsG9ZDF0hz1ZPFTyENtlrDrDS3qmRRqhICBg=
|
||||
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
|
||||
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
@ -1,5 +0,0 @@
|
||||
#!/bin/sh
|
||||
set -ex
|
||||
cd "$(dirname "$0")"
|
||||
GOBIN=$HOME/.terraform.d/plugins GO111MODULE=on go get github.com/yi-jiayu/terraform-provider-telegram
|
||||
terraform init
|
@ -1,30 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"os"
|
||||
|
||||
"github.com/aws/aws-lambda-go/events"
|
||||
"github.com/aws/aws-lambda-go/lambda"
|
||||
tb "gopkg.in/tucnak/telebot.v2"
|
||||
)
|
||||
|
||||
func main() {
|
||||
b, err := tele.NewBot(tele.Settings{
|
||||
Token: os.Getenv("TELEBOT_SECRET"),
|
||||
Synchronous: true,
|
||||
})
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
b.Handle(tele.OnText, func(m *tele.Message) { b.Send(m.Chat, m.Text) })
|
||||
|
||||
lambda.Start(func(req events.APIGatewayProxyRequest) (err error) {
|
||||
var u tele.Update
|
||||
if err = json.Unmarshal([]byte(req.Body), &u); err == nil {
|
||||
b.ProcessUpdate(u)
|
||||
}
|
||||
return
|
||||
})
|
||||
}
|
@ -1,114 +0,0 @@
|
||||
variable "name" {
|
||||
type = string
|
||||
default = "awslambdaechobot" # basename(path.root)
|
||||
description = "Name of the binary (basename of this directory)"
|
||||
}
|
||||
variable "token" {
|
||||
type = string
|
||||
description = "Telegram bot token"
|
||||
}
|
||||
variable "region" {
|
||||
type = string
|
||||
description = "AWS deployment region"
|
||||
}
|
||||
|
||||
locals {
|
||||
exe = var.name
|
||||
zip = "${local.exe}.zip"
|
||||
func = "${var.name}Function"
|
||||
api = "${var.name}API"
|
||||
role = "${var.name}Role"
|
||||
policy = "${var.name}Policy"
|
||||
perm = "${var.name}Permission"
|
||||
log = "/aws/lambda/${local.func}"
|
||||
envToken = "TELEBOT_SECRET"
|
||||
}
|
||||
|
||||
# Run ./init.sh to install github.com/yi-jiayu/terraform-provider-telegram
|
||||
provider "telegram" {
|
||||
bot_token = var.token
|
||||
}
|
||||
resource "telegram_bot_webhook" "a" {
|
||||
url = aws_apigatewayv2_api.a.api_endpoint
|
||||
max_connections = 100
|
||||
}
|
||||
|
||||
provider "archive" {
|
||||
version = "~> 1.3"
|
||||
}
|
||||
data "archive_file" "a" {
|
||||
type = "zip"
|
||||
source_file = local.exe
|
||||
output_path = local.zip
|
||||
}
|
||||
|
||||
provider "aws" {
|
||||
version = "~> 2.59"
|
||||
region = var.region
|
||||
}
|
||||
|
||||
resource "aws_apigatewayv2_api" "a" {
|
||||
name = local.api
|
||||
protocol_type = "HTTP"
|
||||
target = aws_lambda_function.a.arn
|
||||
route_key = "POST /"
|
||||
}
|
||||
resource "aws_lambda_permission" "a" {
|
||||
statement_id = local.perm
|
||||
function_name = aws_lambda_function.a.function_name
|
||||
action = "lambda:InvokeFunction"
|
||||
principal = "apigateway.amazonaws.com"
|
||||
source_arn = "${aws_apigatewayv2_api.a.execution_arn}/*/*/*" # Any stage, method, resource.
|
||||
}
|
||||
|
||||
resource "aws_lambda_function" "a" {
|
||||
function_name = local.func
|
||||
runtime = "go1.x"
|
||||
handler = local.exe
|
||||
memory_size = 128 # MB, 128 + 64*x
|
||||
timeout = 60 # seconds
|
||||
role = aws_iam_role.a.arn
|
||||
|
||||
filename = data.archive_file.a.output_path
|
||||
source_code_hash = data.archive_file.a.output_base64sha256
|
||||
|
||||
environment {
|
||||
variables = {
|
||||
(local.envToken) = var.token
|
||||
}
|
||||
}
|
||||
}
|
||||
resource "aws_cloudwatch_log_group" "a" {
|
||||
# AWS Lambda automatically logs to the group with this name.
|
||||
name = local.log
|
||||
retention_in_days = 1
|
||||
}
|
||||
|
||||
resource "aws_iam_role_policy_attachment" "a" {
|
||||
role = aws_iam_role.a.name
|
||||
policy_arn = aws_iam_policy.a.arn
|
||||
}
|
||||
resource "aws_iam_role" "a" {
|
||||
name = local.role
|
||||
assume_role_policy = data.aws_iam_policy_document.assume_role_policy.json
|
||||
}
|
||||
data "aws_iam_policy_document" "assume_role_policy" {
|
||||
statement {
|
||||
actions = ["sts:AssumeRole"]
|
||||
principals {
|
||||
type = "Service"
|
||||
identifiers = ["lambda.amazonaws.com"]
|
||||
}
|
||||
}
|
||||
}
|
||||
resource "aws_iam_policy" "a" {
|
||||
name = local.policy
|
||||
policy = data.aws_iam_policy_document.a.json
|
||||
}
|
||||
data "aws_iam_policy_document" "a" {
|
||||
# Based on AWSLambdaBasicExecutionRole.
|
||||
statement {
|
||||
actions = ["logs:CreateLogStream", "logs:PutLogEvents"]
|
||||
resources = [aws_cloudwatch_log_group.a.arn]
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue