-
-
Save balloob/d59cae89d19a14bcec99ce1bde05bd44 to your computer and use it in GitHub Desktop.
Telegram Bot that talks to conversation integration
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Create as custom_components/telegram_bot_conversation/__init__.py | |
# Requires telegram_bot to be set up. | |
from homeassistant import core | |
from homeassistant.components.telegram_bot import ( | |
EVENT_TELEGRAM_TEXT, | |
ATTR_TEXT, | |
SERVICE_SEND_MESSAGE, | |
DOMAIN as TELEGRAM_DOMAIN, | |
ATTR_MESSAGE, | |
ATTR_TARGET, | |
ATTR_USER_ID, | |
ATTR_CHAT_ID, | |
) | |
from homeassistant.components.conversation import process | |
DOMAIN = "telegram_bot_conversation" | |
async def async_setup(hass: core.HomeAssistant, config: dict) -> bool: | |
async def text_events(event: core.Event): | |
# Only deal with private chats. | |
if event.data[ATTR_CHAT_ID] != event.data[ATTR_USER_ID]: | |
return | |
response = await process(hass, event.data[ATTR_TEXT], "telegram-bot") | |
await hass.services.async_call( | |
TELEGRAM_DOMAIN, | |
SERVICE_SEND_MESSAGE, | |
{ | |
ATTR_MESSAGE: response.speech["plain"]["speech"], | |
ATTR_TARGET: event.data[ATTR_USER_ID], | |
}, | |
) | |
hass.bus.async_listen(EVENT_TELEGRAM_TEXT, text_events) | |
return True |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"domain": "telegram_bot_conversation", | |
"name": "Telegram Bot <> Conversation", | |
"config_flow": false, | |
"documentation": "", | |
"requirements": [], | |
"dependencies": ["telegram_bot", "conversation"], | |
"codeowners": [] | |
} |
@balloob here's an updated version that works on recent HA releases:
# Create as custom_components/telegram_bot_conversation/__init__.py
# Requires telegram_bot to be set up.
from homeassistant import core
from homeassistant.components.telegram_bot import (
EVENT_TELEGRAM_TEXT,
ATTR_TEXT,
SERVICE_SEND_MESSAGE,
DOMAIN as TELEGRAM_DOMAIN,
ATTR_MESSAGE,
ATTR_TARGET,
ATTR_USER_ID,
ATTR_CHAT_ID,
)
from homeassistant.components.conversation import _async_converse
DOMAIN = "telegram_bot_conversation"
async def async_setup(hass: core.HomeAssistant, config: dict) -> bool:
async def text_events(event: core.Event):
# Only deal with private chats.
if event.data[ATTR_CHAT_ID] != event.data[ATTR_USER_ID]:
return
# Process the conversation intent
response = await _async_converse(hass, event.data[ATTR_TEXT], event.data[ATTR_USER_ID], "telegram-bot")
# Deliver the response back
await hass.services.async_call(
TELEGRAM_DOMAIN,
SERVICE_SEND_MESSAGE,
{
ATTR_MESSAGE: response.speech["plain"]["speech"],
ATTR_TARGET: event.data[ATTR_USER_ID],
},
)
# Subscribe to Telegram text events
hass.bus.async_listen(EVENT_TELEGRAM_TEXT, text_events)
return True
I took also the chance to pass the user ID as the conversation identifier, so that Almond can have separate conversations with different users without messing up 😉
Is there any info/example on how to configure it with the telegram bot? How do I pass message content to Almond and back?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@balloob
homeassistant.components.conversation.process
no longer exists in recent HA releases 😢