Skip to content

Instantly share code, notes, and snippets.

@col
Last active March 28, 2018 09:57
Show Gist options
  • Save col/3d77f656aa557b22aa9f2a740b5fd3dc to your computer and use it in GitHub Desktop.
Save col/3d77f656aa557b22aa9f2a740b5fd3dc to your computer and use it in GitHub Desktop.
Building a golang Telegram bot

Building a golang Telegram bot

I've decided to dive into golang. I usually learn best when I try to build something so I've decided to re-write a Telegram bot I created a few years ago called the WhosInBot. It gets used by quite a few groups here in Singapore ano who know where else. I've had a few issues with the server it's running on recently so the idea of converting it to an AWS Lambda function (which recently released support for golang!) was appealing.

I've had some experience using the serverless framework in the past and found it pretty nice so I thought I'd start there.

Links:

Dependencies:

  • Node 6+ brew install node
  • Golang 1.10+ brew install go
  • AWS CLI brew install awscli
  • Git

Step 1: Create the initial project

npm install -g serverless
cd ${GOPATH}/src
serverless create -t aws-go-dep -p whoinbot
cd whoinbot
git init
git add .
git commit -m "initial commit"

The serverless golang template requires the dep package manager so we also need to install that.

brew install dep
make

Ok great it build correctly but I'd like to make a few changes before doing the first deploy.

Firstly I need to configure my AWS credentials. In my case I just needed to update serverless.yml to specify the correct profile. I also specified the ap-southeast-1 region (a.k.a Singapore).

provider:
  name: aws
  runtime: go1.x
  region: ap-southeast-1
  profile: col.w.harris 

Next I deleted the world directory as I only need a single function and renamed the other function to telegram.

Then I updated the Makefile.

build:
	dep ensure
	GOOS=linux go build -o bin/telegram telegram/main.go

Next I updated the function config within the serverless.yml to reference new the telegram function and add a http event source. This will cause serverless to configure a web endpoint to access the lambda function.

functions:
  telegram:
    handler: bin/telegram
    events:
      - http:
          path: telegram
          method: post
          cors: true

then finally the first deployment

sls deploy
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment