Deploying a Serverless Youtube-To-Gif Telegram bot with OpenFaaS

Eric Stoekl
Sep 26, 2017 · 6 min read
Because it’s running OpenFaaS, you can replace the EC2 instance with any other kind of server imaginable. Raspberry Pi, bare-metal, etc.
Create a GIF from any YouTube video

1. Create the Telegram Bot

2. Deploy OpenFaaS on EC2

Select the ‘Ubuntu Server 16.04’ AMI
#cloud-config
repo_update: true
repo_upgrade: all

runcmd:
- curl -sSL https://get.docker.com/ | sudo sh
- sudo usermod -a -G docker ubuntu
- docker swarm init
- cd /tmp
- git clone -b higherTimeout https://github.com/ericstoekl/faas/
- cd ./faas
- ./deploy_stack.sh
- cd /tmp
- curl -sSL https://cli.openfaas.com | sudo sh
OpenFaaS listens on port 8080, so keeping this port open is essential.
$ ssh -i <your-key-pair.pem> ubuntu@<your-ec2-public-dns>
Pass some markdown to the ‘func_markdown’ function and hit ‘Invoke’ to test that your OpenFaaS deployment is up and running

3. Deploy the core functions of the Youtube-To-Gif Telegram bot on OpenFaaS

~ $ git clone https://github.com/ericstoekl/tg-youtubegif
~ $ cd tg-youtubegif
functions:
telegram-gif-bot:
lang: node
handler: ./telegram-gif-bot
image: ems5311/telegram-gif-bot:0.2
environment:
BOT_TOKEN: <YOUR TOKEN HERE>
write_timeout: 65
read_timeout: 65
write_debug: true
~/tg-youtubegif $ faas-cli build -f tg-youtubegif.yml
~/tg-youtubegif $ faas-cli deploy -f tg-youtubegif.yml
curl <your-AWS-public-DNS>:8080/function/youtube-dl -d "https://www.youtube.com/watch?v=NtgtMQwr3Ko" | curl -X POST <your-AWS-public-DNS>:8080/function/gif-maker --data-binary @- > yt.gif
If you get a file containing this, congrats, your OpenFaaS deployment works!

4. Create the API Gateway

The Telegram bot will send messages to this gateway through HTTP POST requests
$ curl -XPOST https://api.telegram.org/bot<YOUR_BOT_API_KEY>/setWebhook\?url\=<YOUR_API_GATEWAY_ENDPOINT>
{"ok":true,"result":true,"description":"Webhook was set"}
If you see this result when you paste a YouTube video to your Telegram bot, congratulations!

Conclusion

 by the author.

Eric Stoekl

Written by

Fan of Open Source and DevOps. OpenFaaS contributor.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade