How to Control Raspberry Pi Remotely from Anywhere in The World – Telegram Bot with Python

In this tutorial, you will learn how to create a telegram bot to use with python scripts.

This tutorial will allow you to control your Raspberry Pi remotely from anywhere in the world. All you need is an internet connection!

Social networks are often used as an alternative method of communication for electronic projects. It can be an excellent solution for a quick approach to sending instant messages, pictures, or commands remotely.

Telegram is an open-source instant messaging service. It became popular due to the emergence of chat groups with automation possibility. Telegram API is open for integration and it is possible to easily create functional solutions through Telegram.

Bot Setup in Telegram

Go to the telegram search bar and search for “BotFather“:

Once the BothFather screen opens, type the command “/start“:

To create a new bot, type the command “/newbot” into the chat:

Note: Save the “access token”, it will be the key responsible for communication in the next steps.

Setup Raspberry Pi

If you already used the raspberry pi via SSH before, skip this tutorial for the next step. (You can read the detailed tutorial here.)

Open Putty

Connect Pi with SSH

Install “Pip Installs Packages”

sudo apt-get install python-pip

Install “Telepot”

sudo pip install telepot

Now it is necessary to create the executable on the raspberry.

Create a program named “telegrambot” using the code:

sudo nano

Paste the code below:

# Import the required libraries
import sys
import time
import random
import datetime
import telepot
import RPi.GPIO as GPIO
import picamera

# Set warnings to false to avoid error messages 
Pin numbers of the Raspberry PI configured in BOARD mode.
# Set pin 13 as output
GPIO.setup(13, GPIO.OUT)
def On(pin):
def Off(pin):

# Commands that come from the Telegram and are processed by the Raspberry PI 
def handle(msg):
chat_id = msg['chat']['id']
command = msg['text']
print ('got command: %s' % command)
if command == 'On':
    bot.sendMessage(chat_id, On(13))
if command =='Off':
    bot.sendMessage(chat_id, Off(13)) 
# Allows commands typed in telegram to be sent to Raspberry PI
bot = telepot.Bot('Put here your Acess Token')
print(Waiting Code...')
while True:

Run the Code

sudo python

Connect an LED to Rapsberry Pi

Find your bot’s chat window and send the commands you previously configured:

You can use this bot for many different functions on your Raspberry Pi. You can remotely take pictures, connect stream on rpi camera, open gates/doors. Now it’s a simple imagination question!

Have fun!


[1] –

[2] –

Leave a Reply

Your email address will not be published. Required fields are marked *

Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.