Skip to content

Instantly share code, notes, and snippets.

@balloob
Last active July 22, 2022 15:01
Show Gist options
  • Save balloob/d59cae89d19a14bcec99ce1bde05bd44 to your computer and use it in GitHub Desktop.
Save balloob/d59cae89d19a14bcec99ce1bde05bd44 to your computer and use it in GitHub Desktop.
Telegram Bot that talks to conversation integration
# 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
{
"domain": "telegram_bot_conversation",
"name": "Telegram Bot <> Conversation",
"config_flow": false,
"documentation": "",
"requirements": [],
"dependencies": ["telegram_bot", "conversation"],
"codeowners": []
}
@aesedepece
Copy link

@balloob homeassistant.components.conversation.process no longer exists in recent HA releases 😢

@aesedepece
Copy link

@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 😉

@radinsky
Copy link

radinsky commented Dec 2, 2020

Is there any info/example on how to configure it with the telegram bot? How do I pass message content to Almond and back?

@radinsky
Copy link

radinsky commented Dec 2, 2020

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment