Skip to content
Advertisement

Reply to a Python Telegram bot message

Please tell me how you can call the bot’s response to any message in the feedback bot. By example:

User: /start

Bot: Welcome message (Hello)

User: any message

Bot: Feedback message (Expect an answer)

I tried to do it through the echo function, but most likely I made a mistake somewhere.

I am attaching the code:

main.py:

from telegram.ext import Updater

from handlers import setup_dispatcher
from settings import TELEGRAM_TOKEN

# Setup bot handlers
updater = Updater(TELEGRAM_TOKEN)

dp = updater.dispatcher
dp = setup_dispatcher(dp)

updater.start_polling()
updater.idle()

settings.py:

import os
from dotenv import load_dotenv, find_dotenv

# Loading .env variables
load_dotenv(find_dotenv())

TELEGRAM_TOKEN = "HERE TELEGRAM TOKEN"

TELEGRAM_SUPPORT_CHAT_ID = "HERE CHAT ID"
TELEGRAM_SUPPORT_CHAT_ID = int(TELEGRAM_SUPPORT_CHAT_ID)

WELCOME_MESSAGE = os.getenv("WELCOME_MESSAGE", "Hi ๐Ÿ‘‹")
FEEDBACK_MESSAGE = os.getenv("FEEDBACK_MESSAGE", "Expect an answer๐Ÿ‘‹")
REPLY_TO_THIS_MESSAGE = os.getenv("REPLY_TO_THIS_MESSAGE", "REPLY_TO_THIS")
WRONG_REPLY = os.getenv("WRONG_REPLY", "WRONG_REPLY")

handlers.py:

import os

from telegram import Update
from telegram.ext import CommandHandler, MessageHandler, Filters, CallbackContext

from settings import WELCOME_MESSAGE, TELEGRAM_SUPPORT_CHAT_ID, REPLY_TO_THIS_MESSAGE, WRONG_REPLY


def start(update, context):
    update.message.reply_text(WELCOME_MESSAGE) # response to /start

    user_info = update.message.from_user.to_dict()


    context.bot.send_message(
        chat_id=TELEGRAM_SUPPORT_CHAT_ID,
        text=f"""
๐Ÿ“ž Connected {user_info}.
        """,
    )

def echo(update: Update, context: CallbackContext):
    update.message.reply_text(FEEDBACK_MESSAGE) # there should be a response to any message "Expect an answer"

def forward_to_chat(update, context):
    forwarded = update.message.forward(chat_id=TELEGRAM_SUPPORT_CHAT_ID)
    if not forwarded.forward_from:
        context.bot.send_message(
            chat_id=TELEGRAM_SUPPORT_CHAT_ID,
            reply_to_message_id=forwarded.message_id,
            text=f'{update.message.from_user.id}n{REPLY_TO_THIS_MESSAGE}'
        )


def forward_to_user(update, context):
    user_id = None
    if update.message.reply_to_message.forward_from:
        user_id = update.message.reply_to_message.forward_from.id
    elif REPLY_TO_THIS_MESSAGE in update.message.reply_to_message.text:
        try:
            user_id = int(update.message.reply_to_message.text.split('n')[0])
        except ValueError:
            user_id = None
    if user_id:
        context.bot.copy_message(
            message_id=update.message.message_id,
            chat_id=user_id,
            from_chat_id=update.message.chat_id
        )
    else:
        context.bot.send_message(
            chat_id=TELEGRAM_SUPPORT_CHAT_ID,
            text=WRONG_REPLY
        )


def setup_dispatcher(dp):
    dp.add_handler(CommandHandler('start', start))
    dp.add_handler(MessageHandler(Filters.chat_type.private, forward_to_chat))
    dp.add_handler(MessageHandler(Filters.chat(TELEGRAM_SUPPORT_CHAT_ID) & Filters.reply, forward_to_user))
    return dp

I will be very grateful for your help

Advertisement

Answer

Thanks to Aditya Yadav for the advice :)

Additionally, I researched the documentation, regarding the dialog handler part of the library I use. The following solution was found: Rewrite the def echo and def forward_to_chat functions, combining them.

def echo_forward_handler(update, context):
    text = 'Expect an answer'
    context.bot.send_message(chat_id=update.effective_chat.id,
                             text=text)
    forwarded = update.message.forward(chat_id=TELEGRAM_SUPPORT_CHAT_ID)
    if not forwarded.forward_from:
        context.bot.send_message(
            chat_id=TELEGRAM_SUPPORT_CHAT_ID,
            reply_to_message_id=forwarded.message_id,
            text=f'{update.message.from_user.id}n{REPLY_TO_THIS_MESSAGE}'
        )

The necessary dispatcher was also formed at the end:

def setup_dispatcher(dp):
    dp.add_handler(MessageHandler(Filters.text & (~Filters.command) & Filters.chat_type.private, echo_forward_handler))
    return dp

Thus, the task that was set in the condition was solved.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement