I
I
Ivan Ok2019-04-12 16:14:30
Python
Ivan Ok, 2019-04-12 16:14:30

How to forward user messages sent to a bot to another user?

Need advice on telegram chatbot in python
(how to forward messages sent by a user to a bot to another user, and then reply back through the bot)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
Eugene, 2019-04-12
@SwitcherN

Small example:

bot = telebot.TeleBot('Тут должен быть твой токен')


@bot.message_handler(content_types=["text"]) # Тут ловим все текстовые сообщения от пользователя
def some_funtion(message): #Название функции неважно
    bot.send_message('ID чата пользователя, которому необходимо сообщение переслать', message.text)

That is, the send_message method takes 2 positional arguments. The first one is the ID of the chat with the user to send the message to. The second argument is the text of the message to be forwarded. You can slightly modify and replace the line with the following:
...
msg = "Пользователь {} написал \"{}\".".format(message.from_user.username, message.text)
bot.send_message('ID чата пользователя, которому необходимо сообщение переслать', msg)
...

At the output, we get a message like: User Someone wrote "Hello! How are you?".
The chat ID can be obtained using a small function that sends a message:
@bot.message_handler(commands=["id"]) # Получить ID чата при отправке сообщения /id
def chat_id(message):
    my_chat_id = int(message.chat.id)
    bot.send_message(message.chat.id, my_chat_id)

PS But in general - start at least from here: https://groosha.gitbooks.io/telegram-bot-lessons/ .

T
Talyan, 2019-04-12
@flapflapjack

Can you explain how chats work?
In this case, if communication goes through a bot, then the bot is no different from a regular chat.
Bot - chat server, people - people in the chat.
A message sent to another person through a bot is no different from a regular postcard with the addressee's and sender's data sent via Russian mail.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question