'How to use parse mode in telegram send message bot in Python

I'm pretty new to making bot thing so I have several questions I want to ask regarding this:

  1. I'm using a bot to send message by

     def send_message(chat_id, msg):
     url = f"https://api.telegram.org/bot{bot_token}/sendMessage?chat_id={chat_id}&text={msg}"
     requests.get(url)
     send_message(chat_id,msg)
    

By this, I can send message and I want to beautify the result by using parse_mode but I don't know where to put in in url to make it work.

  1. I'm alternate to using Telethon. I can use it to send message to individual and group by user name and group invite link, but when I try to send it to channel by:

    client.send_message(entity='my channel name', message=message)

When I try to run it, it return Cannot find any entity corresponding to "Test channel".

I also try

destination_channel_username='test_ali3'
entity=client.get_entity(destination_channel_username)
client.send_message(entity=entity,message="Hi")

but it require my channel access_hash, how can get it, or are there any other way to send message to channel.

  1. I know Telegram bot API have function like sendMessage or bot.sendMessage that also can do the job, but somehow I can't call it, which packages should I install and/or import.

Thanks a lot!



Solution 1:[1]

  1. This should do the trick:
def send_message(chat_id, msg, parse_mode):
    url = f"https://api.telegram.org/bot{bot_token}/sendMessage?chat_id={chat_id}&text={msg}&parse_mode={parse_mode}"
    requests.get(url)
  1. TBH I'm not too familiar with telethon, but that library is mainly intended for so called userbots. that means that it uses the API that also the Telegram app uses and because of that, it's often times more involved than just the plain bot api.

  2. To use the Bot API in Python, I can recommend the package python-telegram-bot, see https://python-telegram-bot.org. Disclaimer: I'm currently the maintainer of that package. There are also other python packages for the bot api, see e.g. https://core.telegram.org/bots/samples#python

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 CallMeStag