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:
JavaScript
x
14
14
1
from telegram.ext import Updater
2
3
from handlers import setup_dispatcher
4
from settings import TELEGRAM_TOKEN
5
6
# Setup bot handlers
7
updater = Updater(TELEGRAM_TOKEN)
8
9
dp = updater.dispatcher
10
dp = setup_dispatcher(dp)
11
12
updater.start_polling()
13
updater.idle()
14
settings.py:
JavaScript
1
16
16
1
import os
2
from dotenv import load_dotenv, find_dotenv
3
4
# Loading .env variables
5
load_dotenv(find_dotenv())
6
7
TELEGRAM_TOKEN = "HERE TELEGRAM TOKEN"
8
9
TELEGRAM_SUPPORT_CHAT_ID = "HERE CHAT ID"
10
TELEGRAM_SUPPORT_CHAT_ID = int(TELEGRAM_SUPPORT_CHAT_ID)
11
12
WELCOME_MESSAGE = os.getenv("WELCOME_MESSAGE", "Hi 👋")
13
FEEDBACK_MESSAGE = os.getenv("FEEDBACK_MESSAGE", "Expect an answer👋")
14
REPLY_TO_THIS_MESSAGE = os.getenv("REPLY_TO_THIS_MESSAGE", "REPLY_TO_THIS")
15
WRONG_REPLY = os.getenv("WRONG_REPLY", "WRONG_REPLY")
16
handlers.py:
JavaScript
1
62
62
1
import os
2
3
from telegram import Update
4
from telegram.ext import CommandHandler, MessageHandler, Filters, CallbackContext
5
6
from settings import WELCOME_MESSAGE, TELEGRAM_SUPPORT_CHAT_ID, REPLY_TO_THIS_MESSAGE, WRONG_REPLY
7
8
9
def start(update, context):
10
update.message.reply_text(WELCOME_MESSAGE) # response to /start
11
12
user_info = update.message.from_user.to_dict()
13
14
15
context.bot.send_message(
16
chat_id=TELEGRAM_SUPPORT_CHAT_ID,
17
text=f"""
18
📞 Connected {user_info}.
19
""",
20
)
21
22
def echo(update: Update, context: CallbackContext):
23
update.message.reply_text(FEEDBACK_MESSAGE) # there should be a response to any message "Expect an answer"
24
25
def forward_to_chat(update, context):
26
forwarded = update.message.forward(chat_id=TELEGRAM_SUPPORT_CHAT_ID)
27
if not forwarded.forward_from:
28
context.bot.send_message(
29
chat_id=TELEGRAM_SUPPORT_CHAT_ID,
30
reply_to_message_id=forwarded.message_id,
31
text=f'{update.message.from_user.id}n{REPLY_TO_THIS_MESSAGE}'
32
)
33
34
35
def forward_to_user(update, context):
36
user_id = None
37
if update.message.reply_to_message.forward_from:
38
user_id = update.message.reply_to_message.forward_from.id
39
elif REPLY_TO_THIS_MESSAGE in update.message.reply_to_message.text:
40
try:
41
user_id = int(update.message.reply_to_message.text.split('n')[0])
42
except ValueError:
43
user_id = None
44
if user_id:
45
context.bot.copy_message(
46
message_id=update.message.message_id,
47
chat_id=user_id,
48
from_chat_id=update.message.chat_id
49
)
50
else:
51
context.bot.send_message(
52
chat_id=TELEGRAM_SUPPORT_CHAT_ID,
53
text=WRONG_REPLY
54
)
55
56
57
def setup_dispatcher(dp):
58
dp.add_handler(CommandHandler('start', start))
59
dp.add_handler(MessageHandler(Filters.chat_type.private, forward_to_chat))
60
dp.add_handler(MessageHandler(Filters.chat(TELEGRAM_SUPPORT_CHAT_ID) & Filters.reply, forward_to_user))
61
return dp
62
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.
JavaScript
1
12
12
1
def echo_forward_handler(update, context):
2
text = 'Expect an answer'
3
context.bot.send_message(chat_id=update.effective_chat.id,
4
text=text)
5
forwarded = update.message.forward(chat_id=TELEGRAM_SUPPORT_CHAT_ID)
6
if not forwarded.forward_from:
7
context.bot.send_message(
8
chat_id=TELEGRAM_SUPPORT_CHAT_ID,
9
reply_to_message_id=forwarded.message_id,
10
text=f'{update.message.from_user.id}n{REPLY_TO_THIS_MESSAGE}'
11
)
12
The necessary dispatcher was also formed at the end:
JavaScript
1
4
1
def setup_dispatcher(dp):
2
dp.add_handler(MessageHandler(Filters.text & (~Filters.command) & Filters.chat_type.private, echo_forward_handler))
3
return dp
4
Thus, the task that was set in the condition was solved.