Pedro Henrique Fonseca Bertoleti
Published © CC BY-SA

...And Telegram for PI!

PI is the most awesome number in the world! What about knowing more about it using Telegram App, anywhere in the world? Check it out here!

BeginnerFull instructions provided1 hour2,856
...And Telegram for PI!

Things used in this project

Hardware components

Raspberry Pi 3 Model B
Raspberry Pi 3 Model B
Any Raspberry Pi w/ Internet connection will work!
Raspberry Pi 2 Model B
Raspberry Pi 2 Model B
Any Raspberry Pi w/ Internet connection will work!
Raspberry Pi Zero
Raspberry Pi Zero
Any Raspberry Pi w/ Internet connection will work!

Software apps and online services

Telegram messenger: use it anywhere!


Read more



Source-code of the project.
There's no secret at all: after getting all PAI and libraries (as described in the tutorial), just paste this code in a Python file and execute it by typing python
#PI Facts bot for Telegram
#Author: Pedro Bertoleti
#Date: Mar/2017

import telegram			
from time import sleep 
from urllib2 import URLError
from random import randint

#global variables
PiFactsCollection = []

#Function: manages PI Telegram bot
#Params: bot object and update ID
#Return: update ID
def PIBot(bot, update_id):
    for update in bot.getUpdates(offset=update_id, timeout=10):
        chat_id = update.message.chat_id
        update_id = update.update_id + 1
        message = update.message.text

        if message:
           CheckAndAnswer(message, bot, chat_id) 

    return update_id

#Function: check message rec. and answer it (if it's needed)
#Params: message, bot telegram object and Telegram chat ID
#Return: none
def CheckAndAnswer(Msg,BotObj,ChatID):
	MsgLowerCase = Msg.lower()

	if (MsgLowerCase == "hi"):
		BotObj.sendMessage(chat_id=ChatID, text="Hi there! Do you wanna know more about PI? Type yes or facts words.")

	if ((MsgLowerCase == "yes") or (MsgLowerCase == "facts")):
		RandomFact = SelectRandonFact()
		BotObj.sendMessage(chat_id=ChatID, text=RandomFact)

#Function: select randon facts about PI
#Params: none
#Return: random Pi fact
def SelectRandonFact():
	global PiFactsCollection

	FactsLen = len(PiFactsCollection)
	RandonFact = randint(0,FactsLen - 1)
	return PiFactsCollection[RandonFact]

#Function: Init PI facts collection
#Params: none
#Return: none
def InitFacts():
	global	PiFactsCollection

	#Facts from:
  #You can put here as many facts you want to! There's almost no limits here.
  #For instance, I've put just three facts.

	PiFactsCollection.append("Pi is the most recognized mathematical constant in the world. Scholars often consider Pi the most important and intriguing number in all of mathematics.")
	PiFactsCollection.append("Scientists in Carl Sagan s novel Contact are able to unravel enough of pi to find hidden messages from the creators of the human race, allowing humans to access deeper levels of universal awareness")
	PiFactsCollection.append("In 1995, Hiroyoki Gotu memorized 42,195 places of pi and is considered the current pi champion. Some scholars speculate that Japanese is better suited than other languages for memorizing sequences of numbers.")


update_id = None

#Telegram API Token. Get yours with BotFather!

bot = telegram.Bot(BotToken)

while True:
            update_id = PIBot(bot, update_id)
        except telegram.TelegramError as e:
            if e.message in ("Bad Gateway", "Timed out"):
                raise e
        except URLError as e:


Pedro Henrique Fonseca Bertoleti

Pedro Henrique Fonseca Bertoleti

8 projects • 41 followers
Hi there! My name is Pedro Bertoleti. I am aboslutely crazy about: - Electronics - Embedded software design - Technology