Do you now the bot in Telegram, right?
Well, this is my new adventure (in Wi-Fi... cit. one of the album of my favorite band...).
Creating a bot seems to be a simple operation and it is!
So I tried to create ones.
Just for starting I used my best project to transpose it in a Telegram bot answering to the following question: if I wanted to know a specific weather parameter of my weather station? I.e. temperature?
Well: the Telegram bot is the answer! (no, not 42!)
- First of all: download Telegram app
- Go to the account @BotFather
- Create your personal bot through the instructions you find in it
- Well... happy hacking!
I chose python to create my personal bot. Take a look at the code, but before of this you have to know that my shield transmits his data to the cloud platform ThingSpeak.com right here: https://thingspeak.com/channels/145284
In this way our bot answer to the request connecting himself to that link and, after analyzed the word received from the user, resend all the main data.
Here is the code:
import sys
import time
import json
from urllib import request
import telepot
from telepot.loop import MessageLoop
def handle(msg):
content_type, chat_type, chat_id = telepot.glance(msg)
print(content_type, chat_type, chat_id, msg)
# immagazzina in una variabile la risposta dal GET response
response = request.urlopen('https://thingspeak.com/channels/145284/feeds.json?results=2')
# preleva i dati json dalla richiesta
data = response.read().decode('utf-8')
# convertiamo la stringa in dizionario
data_dict = json.loads(data)
# separiamo i valori che ci interessano
feeds = data_dict['feeds']
#origin = msg
#origin = msg.read().decode('utf-8')
#data_msg = json.loads(msg)
#dati = data_msg['field1']
#print(msg['text'])
# stampo i valori
text="La Temperatura è di: " + str(float(feeds[1]['field1'])) + " C\n" \
"L'Umidità è il: " + str(float(feeds[1]['field2'])) + " %\n" \
"Il Dew Point è di: " + str(round(float(feeds[1]['field3']),1)) + " C\n" \
"La Pressione è di: " + str(round(float(feeds[1]['field5']),1)) + " hPa"
print(text)
if msg['text'] == 'Meteo' or msg['text'] == 'meteo':
bot.sendMessage(chat_id, text)
#if content_type == 'text':
# bot.sendMessage(chat_id, text)
TOKEN = 'my-token'
bot = telepot.Bot(TOKEN)
MessageLoop(bot, handle).run_as_thread()
print ('Listening ...')
# Keep the program running.
while 1:
time.sleep(10)
Once you access on Telegram you can search for MeteoLeccebot:
And when you ask for the data you have to digit the word Meteo or meteo (weather, in italian) you receive what you have requested:
That's all!