电报bot推荐
您已经完全设置了智能家居。 您经常喜欢与朋友一起炫耀,打开灯泡,播放带语音助手提示的视频和电影,在应用程序上轻按即可煮咖啡和调节恒温器。 恭喜你!
但是,如果您是一个自动化爱好者,很少定居,那么对于控制小工具必须下载的应用程序数量以及必须掌握的界面数量,您可能会感到沮丧。 您可能会有一个用于灯光的应用程序,一个用于媒体中心的应用程序,一个用于智能阴影的应用程序,一个用于恒温器的应用程序,以及一个热情(而且绝望地)试图将所有这些都放置在同一位置的Google Home应用。
这些应用程序中的大多数可能无法与其他应用程序通信,并且如果您与小工具不在同一网络上,则其中许多应用程序将无法运行。 如果我们可以通过可从移动和台式设备以及外部脚本/集成访问的界面,从同一个界面控制所有内容,而不会使我们的手机或装有大量应用程序的计算机杂乱无章,那么您是否在您的家庭网络还是室外? 一个既轻巧又易于使用的界面?
但是,等等,这样的界面不是以消息传递或聊天的名义存在了一段时间吗? 毕竟,通过与我们用来向猫友发送猫图片的界面相同的界面,以及完全根据我们的需求量身定制的机器人来控制我们的房屋,小工具和云服务,这不是很酷,而这并没有那么简单通常带有第三方机器人?
进入由聊天机器人驱动的智能家居的世界。 在这个故事中,我将向您展示如何在现有的智能家居安装之上轻松设置命令和例程。 本教程中使用的两个主要工具是:
Telegram :存在许多消息传递应用程序和平台,但到目前为止,至少可以说,其中许多(Facebook Messenger,Whatsapp,环聊等)在提供可用的开发人员API方面的努力令人失望。 每个人都使用XMPP或IRC作为消息传递主干的黄金日子已经一去不复返了,并且很容易与使用相同语言的服务进行集成:当今的消息传递应用程序世界极为分散。
此外,由于创建许多与世界其他地方不连通的围墙花园符合许多大型企业的利益,因此,最常用的解决方案不附带官方支持的API /开发人员接口。 不仅如此:某些大型公司积极地劝阻用户不要使用除官方应用程序以外的任何其他东西来与平台进行交互( 邪恶的Whatsapp,我在看着你 )。
在这个由多个未连接的岛屿组成的极其分散的世界中,Telegram代表了一个受欢迎的例外:他们的官方bot API得到了充分的文档记录和支持,对于任何了解一些编程的人来说,构建集成都非常容易。
platypush :那些一直关注我一段时间的人可能会听说过我的自动化极客平台(对于那些不喜欢我的人,您可以阅读我对platypush的介绍性文章 , Wiki或集成文档来进行总结)。 platypush还随附Telegram 插件和backend 。
因此,让我们开始吧,制作您的第一个家庭自动化机器人!
创建电报机器人
在Telegram上创建一个新的bot很容易:
- 与BotFather进行对话。
- 键入/ start,然后键入/
/newbot
以创建一个新的bot。 给您的机器人一个显示名称和一个用户名。 - 将为您提供一个链接,以开始与您的漫游器和唯一的API密钥进行对话。 将其存储在某个地方,我们将很快使用它来配置platypush插件。

在platypush中配置您的机器人
1.使用主要扩展以及Telegram集成安装platypush:
pip install 'platypush[http,db,telegram]'
apt-get install redis-server
[sudo] systemctl start redis
[sudo] systemctl enable redis
2. 尝试一下吧。 查找一些您想要管理/自动化的东西-灯光,音乐,媒体,传感器,数据库,机械手,智能插头...-并安装/配置相关的扩展。
在本文中,我们将介绍如何配置新创建的漫游器以控制Philips Hue灯光,音乐播放和PiCamera流。
3.将电报配置添加到您的~/.config/platypush/config.yaml
文件中:
chat.telegram:
api_token: <your bot token>
backend.chat.telegram:
enabled: true
后端使您能够接收事件(例如新消息,附件,请求等)并在事件上创建自定义钩子。 该插件使您可以编写聊天记录,以编程方式发送消息和附件,管理频道等。
假设我们希望机器人实现以下命令:
-
/start
:欢迎新用户。 -
/help
:显示可用命令。 -
/lights_on
:打开灯。 -
/lights_off
:关闭灯。 -
/music_play
:播放音乐资源/ URL。 -
/music_pause
:切换播放暂停状态。 -
/music_next
:播放下一首歌曲。 -
/music_prev
:播放上一首歌曲。 -
/start_streaming
:在PiCamera上开始远程流式传输。 -
/stop_streaming
:在PiCamera上停止远程流。
我们要做的就是在platypush config.yaml中创建事件挂钩。 在这种情况下,您需要:
安装并配置Philips Hue , mopidy和PiCamera插件:
pip install 'platypush[hue,mpd,picamera]'
# Hue lights configuration
light.hue:
# Hue bridge IP address
bridge: 192.168 .1 .10
# Default groups to control
groups:
- Living Room
# MPD/Mopidy configuration
music.mpd:
host: localhost
port: 6600
# PiCamera configuration
camera.pi:
vflip: False
hflip: False
为了使config.yaml
保持清洁,请创建一个名为~/.config/platypush/include/bot.yaml
的新文件:
# /start command handler
event.hook.OnTelegramStartCmd:
if:
type: platypush.message.event.chat.telegram.CommandMessageEvent
command: start
then:
- action: chat.telegram.send_message
args:
chat_id: ${chat_id}
text: "Welcome! Type /help to see the available commands"
# /help command handler
event.hook.OnTelegramHelpCmd:
if:
type: platypush.message.event.chat.telegram.CommandMessageEvent
command: help
then:
- action: chat.telegram.send_message
args:
chat_id: ${chat_id}
text: "Available commands:\n
- /lights_on\n
- /lights_off\n
- /music_play [resource]\n
- /music_pause\n
- /music_prev\n
- /music_next\n
- /start_streaming\n
- /stop_streaming\n
"
# /lights_on command handler
event.hook.OnTelegramLightsOnCmd:
if:
type: platypush.message.event.chat.telegram.CommandMessageEvent
command: lights_on
then:
- action: light.hue.on
- action: chat.telegram.send_message
args:
chat_id: ${chat_id}
text: "Lights turned on"
# /lights_off command handler
event.hook.OnTelegramLightsOffCmd:
if:
type: platypush.message.event.chat.telegram.CommandMessageEvent
command: lights_off
then:
- action: light.hue.off
- action: chat.telegram.send_message
args:
chat_id: ${chat_id}
text: "Lights turned off"
# /music_play command handler
event.hook.OnTelegramMusicPlayCmd:
if:
type: platypush.message.event.chat.telegram.CommandMessageEvent
command: music_play
then:
- if ${cmdargs}:
- action: music.mpd.play
args:
resource: cmdargs[0]
- else:
- action: music.mpd.play
- action: chat.telegram.send_message
args:
chat_id: ${chat_id}
text: "Music playing"
# /music_pause command handler
event.hook.OnTelegramMusicPauseCmd:
if:
type: platypush.message.event.chat.telegram.CommandMessageEvent
command: music_pause
then:
- action: music.mpd.pause
- action: chat.telegram.send_message
args:
chat_id: ${chat_id}
text: "Music paused"
# /music_prev command handler
event.hook.OnTelegramMusicPrevCmd:
if:
type: platypush.message.event.chat.telegram.CommandMessageEvent
command: music_prev
then:
- action: music.mpd.previous
- action: chat.telegram.send_message
args:
chat_id: ${chat_id}
text: "Playing previous track"
# /music_next command handler
event.hook.OnTelegramMusicNextCmd:
if:
type: platypush.message.event.chat.telegram.CommandMessageEvent
command: music_next
then:
- action: music.mpd.next
- action: chat.telegram.send_message
args:
chat_id: ${chat_id}
text: "Playing next track"
# /start_streaming command handler
event.hook.OnTelegramCameraStartStreamingCmd:
if:
type: platypush.message.event.chat.telegram.CommandMessageEvent
command: start_streaming
then:
- action: camera.pi.start_streaming
args:
listen_port: 2222
- action: chat.telegram.send_message
args:
chat_id: ${chat_id}
text: "PiCamera streaming started. Check it out with vlc tcp/h264://hostname:2222"
# /stop_streaming command handler
event.hook.OnTelegramCameraStopStreamingCmd:
if:
type: platypush.message.event.chat.telegram.CommandMessageEvent
command: stop_streaming
then:
- action: camera.pi.stop_streaming
- action: chat.telegram.send_message
args:
chat_id: ${chat_id}
text: "PiCamera streaming stopped"
在config.yaml
包含您的机器人配置:
include:
- include/bot.yaml
启动platypush:
# Manual start
platypush
# Service start
systemctl start platypush.service
通过BotFather提供的链接在Telegram上与您的机器人进行对话,然后开始玩:

现在,任何人都可以访问该机器人-您可能不希望这样。 您可以配置Telegram后端,以便它仅接受来自特定聊天ID列表的消息(在Telegram中, chat_id用于私人用户和群组)。
向机器人发送消息并打开platypush日志或检查其标准输出。 您应该看到一些这样的消息:
2020 -01 -03 19 : 09 : 32 , 701 | INFO|platypush|Received event: { "type" : "event" , "target" : "turing" , "origin" : "turing" , "id" : "***" , "args" : { "type" : "platypush.message.event.chat.telegram.CommandMessageEvent" , "chat_id" : your_chat_id, "message" : { "text" : "/help" , ...}, "user" : { "user_id" : your_user_id, "username" : "****" , "is_bot" : false , "link" : "https://t.me/you" , "language_code" : "en" , "first_name" : "***" , "last_name" : "***" }, "command" : "help" , "cmdargs" : []}}
在后端配置中复制与您的用户关联的chat_id :
backend.chat.telegram:
authorized_chat_ids:
- your_user_id
如果您尝试从未经授权的用户发送消息,则漫游器现在将以错误答复。
您还可以邀请您的漫游器进行群组聊天,并让您的朋友在您家里闪烁灯光!
接下来是什么?
在本文中,我们仅探讨了Telegram集成的一个特定功能:机器人对CommandMessageEvent事件做出React,运行动作以及用文本消息回复的能力。 从支持的电报事件列表中可以看到,您可以做更多的事情,例如:
- 当某人共享联系人信息时创建钩子-是否曾经想过让机器人自动存储您的朋友通过聊天发送给您的新联系人?
- 共享照片,视频或图像文件时创建钩子,例如自动将发送到聊天的所有媒体文件下载到硬盘驱动器或远程Dropbox文件夹。
- 对文本消息而不是命令运行操作-例如,如果您更喜欢键入“打开灯”而不是“ / lights_on”,则可以使用TextMessageEvent 。
- 从相机拍摄一张照片,然后通过send_photo操作将其发送给自己。您还可以部署多个机器人,例如针对每个设备,以便您可以通过关联的聊天在特定设备上运行操作,或者使用单个机器人作为条目指向消息并通过MQTT , Kafka或HTTP API将消息传递到其他设备。
只要有插件,您现在就可以通过聊天机器人完成。
翻译自: https://hackernoon.com/controlling-your-smart-home-over-a-telegram-bot-3z742xc5
电报bot推荐