I have a telegram bot, used as a bot for user support. I prescribed the ability to reply to users.
But only text is sent, and I want the reply to come in the form of “text + quote of the original message”. That is, as in the correspondence of two people, when we click “Reply”.
Here is my code:
#The part with importing and initializing tokens is omitted here
def save_user_info(user_id, username):
conn = sqlite3.connect('users.db')
cursor = conn.cursor()
cursor.execute('CREATE TABLE IF NOT EXISTS users (user_id INTEGER PRIMARY KEY, username TEXT)')
cursor.execute('INSERT OR REPLACE INTO users (user_id, username) VALUES (?, ?)', (user_id, username))
conn.commit()
conn.close()
@bot.message_handler(content_types=['text'])
def handle_text(message):
save_user_info(message.chat.id, message.chat.username)
bot.forward_message(YOUR_TELEGRAM_ID, message.chat.id, message.message_id)
if message.reply_to_message:
original_chat_id = message.reply_to_message.forward_from.id
bot.send_message(original_chat_id, message.text)
def forward_content_to_user(message, user_id):
if message.content_type == 'photo':
file_id = message.photo[-1].file_id
caption = message.caption if message.caption else ''
bot.send_photo(user_id, file_id, caption=caption)
elif message.content_type == 'video':
file_id = message.video.file_id
caption = message.caption if message.caption else ''
bot.send_video(user_id, file_id, caption=caption)
elif message.content_type == 'document':
file_id = message.document.file_id
caption = message.caption if message.caption else ''
bot.send_document(user_id, file_id, caption=caption)
elif message.content_type == 'audio':
file_id = message.audio.file_id
caption = message.caption if message.caption else ''
bot.send_audio(user_id, file_id, caption=caption)
elif message.content_type == 'sticker':
file_id = message.sticker.file_id
bot.send_sticker(user_id, file_id)
elif message.content_type == 'voice':
file_id = message.voice.file_id
bot.send_voice(user_id, file_id)
elif message.content_type == 'location':
latitude = message.location.latitude
longitude = message.location.longitude
bot.send_location(user_id, latitude, longitude)
elif message.content_type == 'contact':
phone_number = message.contact.phone_number
first_name = message.contact.first_name
bot.send_contact(user_id, phone_number, first_name)
@bot.message_handler(content_types=['photo', 'video', 'document', 'audio', 'sticker', 'voice', 'location', 'contact'])
def handle_content(message):
save_user_info(message.chat.id, message.chat.username)
bot.forward_message(YOUR_TELEGRAM_ID, message.chat.id, message.message_id)
if message.reply_to_message:
original_chat_id = message.reply_to_message.forward_from.id
forward_content_to_user(message, original_chat_id)
bot.polling()
I tried using reply_to_mesage, but the bot quoted the messages to myself, not the user.
New contributor
Сергей Ижутов is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.