Python-Telegram-Bot

Python and python framework

Moderator: leecollings

Post Reply
User avatar
emme
Posts: 909
Joined: Monday 27 June 2016 11:02
Target OS: Raspberry Pi / ODroid
Domoticz version: latest
Location: Milano, Italy
Contact:

Python-Telegram-Bot

Post by emme »

Ciao,

in the process to continue the automation and integration of my home, @bigpea and myself jumped into the python-telegram-bot library for which I did not see any reference in the wiki.
I would like to share with you my experience about... in order to let you know there is a new simple way to interact with :P

let me tell you that I found this extreamely easy to use (I'm not a real programmer and I have low knowledge in python) and really fast!

you need to have a python script working as a service and you can interact with domoticz using the http json method (unless someone wouldn't like to create a pluging with it :P :P)

for example, once you have created your bot in telegram, to get the camera snapshot in a faster way, here is the code:

Code: Select all

import os
import telegram
import urllib.request
from telegram.ext import Updater, CommandHandler
APIKEY = 'put here your telegram BOT Token'

bot = telegram.Bot(token=APIKEY)
updater=Updater(token=APIKEY)
dispatcher = updater.dispatcher

def sendSnap(bot, update):
       os.system('wget -O /home/pi/snap1.jpg http://127.0.0.1:8080/camsnapshot.jpg?idx=1')
       bot.send_photo(chat_id=update.message.chat_id, photo=open('/home/pi/snap1.jpg', 'rb'), text="Ecco l'immagine che mi hai richiesto")

dispatcher.add_handler(CommandHandler('cam', sendSnap))
updater.start_polling()
updater.idle()
       
now you can run it (don't forget to sudo chmod +x yourFileName.py), go into your telegram app and start a conversation with your bot..
issue a /cam and you'll get it!...
that's only an example but there are a lots of possibile integration!
I have a generic status report for my entire home, camera screenshor selection and a help screen... and more will come!
The most dangerous phrase in any language is:
"We always done this way"
User avatar
papoo
Posts: 126
Joined: Friday 22 January 2016 22:14
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.10
Location: France
Contact:

Re: Python-Telegram-Bot

Post by papoo »

do you use other scripts that can help us get started?
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest