-2

I'm setting up a Telegram Bot, everything works fine but now i would like the bot to answer in private chat for a specific command which was asked in a group. I used Python 3.x.

How can I get the Chat ID of the Private Chat between the Bot and the user?

Felix
  • 33
  • 1
  • 10

4 Answers4

3

Telegram API names it as chat_id. You can send message to chat_id which goes to private chat.

Keep in mind that bots can't start a conversation(chat dialog, private chat), unless a user starts it first.

ramazan polat
  • 7,111
  • 1
  • 48
  • 76
  • But if i make `chat_id=update.message.chat_id` the bot answers in the group Chat – Felix Jul 10 '18 at 13:32
  • 1
    If your bot doesn't have any private conversation with a particular user, then you can't send a message to them. – ramazan polat Jul 10 '18 at 13:35
  • I know but if there was a conversation with the user, how can I get the chat_id – Felix Jul 10 '18 at 13:46
  • @WlanWerner get the chat_id of the conversation, that's it. – ramazan polat Jul 10 '18 at 14:10
  • @RamazanPolat Is there any way to find out if there has been previous contact? (other than to log every convo?) – Kraay89 Feb 26 '19 at 20:41
  • 1
    @Kraay89, you don't log convos, Telegram already has your convos. First you get dialogs. Each dialog has participant chat_ids. If you don't have any dialog, then you can't get their chat_id, therefore can't talk with them. – ramazan polat Feb 27 '19 at 00:03
2

The Update contains a message, that tells you who sent it. The chat_id of the user is equal to the id of the User object.

You can retrieve it as following:

chat_id = update.message.from.id
Maak
  • 4,720
  • 3
  • 28
  • 39
0

If you want to answer a user in pm (switch from group to pm)...

Use : text = "http://telegram.me/<Bot_Username>?start=start"

replace '<Bot_Username>' with *your bots username (dont include the "<>") ,

eg: " http://telegram.me/jesvi_bot?start=start " would take me straight to my bot's pm

send this as a reply text/link in the target group & upon the target user clicking it, it would re direct them to the bots private message..

So a full Example would be :

update.message.reply_text(text="http://telegram.me/jesvi_bot?start=start")

where 'jesvi_bot' in this case refers to my bot's username & replace it with your bot's username.

0

Let's take a look at this piece of code:

def bot_func(update, context):

    update.message.reply_text(text='chunk',
                              parse_mode='HTML')

    context.bot.send_message(chat_id=update.message.from_user.id,
                             text='chunk',
                             parse_mode='HTML')

The first method will reply in current chat and simply accept the chat_id as is, regardless whether the chat_id belongs to a private chat or a group chat:

    update.message.reply_text(text='chunk',
                              parse_mode='HTML')

Force Reply as a private chat

The second method will force reply as a private chat, as the message is directy send to the user specified in update.message.from_user.id:

    context.bot.send_message(chat_id=update.message.from_user.id,
                             text='chunk',
                             parse_mode='HTML')
Jansen Simanullang
  • 1,405
  • 14
  • 11