-1

I'm use python-telegram-bot for sending message to users (in private messages). In the documentation wrote, that bot can send message to user, knowing his channel_id or username.

But, by chat_id message is sending, but by username I get error telegram.error.Bad Request: Chat not found `

bot.send_message(chat_id='@username', text)
Proteeti Prova
  • 1,079
  • 4
  • 25
  • 49
  • 1
    Did not test but as I see from docs: "chat_id (int | str) – Unique identifier for the target chat or username of the target channel (in the format @channelusername)." - you can send messages by name to channels, not users – Vladimir Kolenov Oct 11 '19 at 09:13
  • But why can I send a `private message` to the user via the `chat_id` with the user, but not to his `username`? –  Oct 11 '19 at 09:29

2 Answers2

0

Here it is quoted

chat_id (:obj:int | :obj:str): Unique identifier for the target chat or username of the target channel (in the format @channelusername).

    @log
    def send_message(self,
                     chat_id,
                     text,
                     parse_mode=None,
                     disable_web_page_preview=None,
                     disable_notification=False,
                     reply_to_message_id=None,
                     reply_markup=None,
                     timeout=None,
                     **kwargs):
        """Use this method to send text messages.
        Args:
            chat_id (:obj:`int` | :obj:`str`): Unique identifier for the target chat or username
                of the target channel (in the format @channelusername).
            text (:obj:`str`): Text of the message to be sent. Max 4096 characters. Also found as
                :attr:`telegram.constants.MAX_MESSAGE_LENGTH`.
            parse_mode (:obj:`str`): Send Markdown or HTML, if you want Telegram apps to show bold,
                italic, fixed-width text or inline URLs in your bot's message. See the constants in
                :class:`telegram.ParseMode` for the available modes.
            disable_web_page_preview (:obj:`bool`, optional): Disables link previews for links in
                this message.
            disable_notification (:obj:`bool`, optional): Sends the message silently. Users will
                receive a notification with no sound.
            reply_to_message_id (:obj:`int`, optional): If the message is a reply, ID of the
                original message.
            reply_markup (:class:`telegram.ReplyMarkup`, optional): Additional interface options.
                A JSON-serialized object for an inline keyboard, custom reply keyboard,
                instructions to remove reply keyboard or to force a reply from the user.
            timeout (:obj:`int` | :obj:`float`, optional): If this value is specified, use it as
                the read timeout from the server (instead of the one specified during creation of
                the connection pool).
            **kwargs (:obj:`dict`): Arbitrary keyword arguments.
        Returns:
            :class:`telegram.Message`: On success, the sent message is returned.
        Raises:
            :class:`telegram.TelegramError`
        """
        url = '{0}/sendMessage'.format(self.base_url)

        data = {'chat_id': chat_id, 'text': text}

        if parse_mode:
            data['parse_mode'] = parse_mode
        if disable_web_page_preview:
            data['disable_web_page_preview'] = disable_web_page_preview

        return self._message(url, data, disable_notification=disable_notification,
                             reply_to_message_id=reply_to_message_id, reply_markup=reply_markup,
                             timeout=timeout, **kwargs)

You should send channel name not the the username of user here.

Yugandhar Chaudhari
  • 3,831
  • 3
  • 24
  • 40
0

Here's my method

def save_user_id(bot, update):
    username = get_username(update)
    tg_user, created = YourModel.objects.get_or_create(username=username,
                                                       first_name=update.message.from_user.first_name,
                                                       telegram_user_id=update.message.from_user.id)

so later you can use username to send a message:

user = YourModel.objects.get(username='@username')
bot.send_message(chat_id=user.telegram_user_id, text='your text')

forgot to add get_username func. It's simple:

def get_username(update):
    username = update.message.from_user.username

but I'd recommend to extend it to your needs, because some of users haven't setup their usernames

Oleksii Dubniak
  • 227
  • 2
  • 7