Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gilangvperdana/faa31621521519bfc219c699a612f872 to your computer and use it in GitHub Desktop.
Save gilangvperdana/faa31621521519bfc219c699a612f872 to your computer and use it in GitHub Desktop.
A short guide for hosting telegram bot on Ubuntu!

How to host a telegram bot on Ubuntu (18.04/20.04)

Introduction

This short guide will walk you through hosting your very own telegram bot on Ubuntu (tested on Ubuntu 18.04 and 20.04)!

Prerequisites

This guide assumes knowledge of the following:

1) Provisioning a VPS
2) Familiarity with SSH
3) Familiarity with linux command line

Server Setup

To begin, you will need to provision a VPS from cloud providers such as digitalocean or upcloud. Other popular services like AWS and google cloud would work as well with their EC2 and compute instances but the nature of those services are such that they are slightly more complicated to work with so they will not be included in this guide.

Once you have your VPS provisioned, SSH into your server with the following command (replacing 11.11.11.11 with your server's IP address):

$ ssh root@11.11.11.11

Within your server, run the update command below:

$ apt-get update

Next, to ensure that your bot will be running 24/7, we will be using screen to keep the process alive even when we are no longer maintaining an SSH connection to the server:

$ apt-get install screen

User Setup

Let us next quickly create a new user that will be handling this telegram bot. For the purpose of this guide, a telegram bot I coded in python, Simple Media Converter will be used which will be handled by the user smc. Run the following command to create this new user:

$ adduser smc

Give the user superuser permissions:

$ usermod -aG sudo smc

For the remaining part of the guide, exit from your root SSH session and login as smc instead (again replacing 11.11.11.11 with the IP address of your VPS):

$ ssh smc@11.11.11.11

Project Setup

This part will vary greatly depending on the nature of your project. For demonstration purposes, if you are following through with the Simple Media Converter project, then follow this setup guide to get the project ready.

Project Launch

Once the project setup is complete, initialize a new screen session with the following command (this is important if you want your bot to be running 24/7):

$ screen

Hit enter to continue and in this new screen, start your telegram bot:

$ python3 main.py

Detach from the screen by hitting ctrl + a + d on your keyboard. Make sure to hit all 3 at once because just hitting ctrl + d alone will terminate your screen instead. To view the list of screens, you may use the following command:

$ screen -ls

To reattach to the screen, simply use the command:

$ screen -r

With that, your bot should now be running 24/7! This concludes the guide for hosting your very own telegram bot on Ubuntu! Thank you for reading!

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