2

I'm doing a GET request using the Telegram Bot API.

I use the following query

telegram_msg = requests.get(f'https://api.telegram.org/<botname>:<botAPI>/sendPhoto?chat_id=<chatID>&parseMode=MarkdownV2&photo={link}&caption=*bold\*{title}*\n\n{res}')

So I want the title to be bold. What am I doing wrong? Can someone help me please?

0stone0
  • 34,288
  • 4
  • 39
  • 64
Laura
  • 41
  • 1
  • 8

1 Answers1

1

The issue is caused by the way you pass the parse_mode.


In your url there is:

&parseMode=MarkdownV2

But that should be

&parse_mode=MarkdownV2

After changing that, it works as expected with the following url/code/output:

https://api.telegram.org/bot<TOKEN>/sendPhoto?chat_id=<CHAT-ID>&parse_mode=MarkdownV2&photo=http://placehold.jp/150x150.png&caption=*Foo*Bar
import requests

chatID='my-chat-id'
link='http://placehold.jp/150x150.png'
token='my-private-token'
caption='Example text'
telegram_msg = requests.get(f'https://api.telegram.org/bot{token}/sendPhoto?chat_id={chatID}&parse_mode=MarkdownV2&photo={link}&caption=*{caption}*')

Note: Make sure you're using Python 3.6 or above to use f strings:

How do I put a variable’s value inside a string?


enter image description here

0stone0
  • 34,288
  • 4
  • 39
  • 64
  • Thank you. It works only when I pass links, but what if I put links and text dynamically. It won't work if I pass {link} and {description} – Laura Apr 20 '22 at 12:54
  • As you currently are in your question? Are you sure you're using python3+. Consider reading [this answer that shows other ways](https://stackoverflow.com/questions/2960772/how-do-i-put-a-variable-s-value-inside-a-string). – 0stone0 Apr 20 '22 at 12:55
  • Am I doing it wrong with f-String? – Laura Apr 20 '22 at 12:59
  • I've added my test-code. f Strings only work if you are using python 3.6 or higher. But the chat/id and bot token are not variables, please see my example. – 0stone0 Apr 20 '22 at 13:02
  • Oh, thank you. Can you please help me how do I put variable with bold text into another variable? text=*link* doesn't work. Should I use any escape character? – Laura Apr 20 '22 at 13:16
  • So you want to use a variable containing some text, to be bold? – 0stone0 Apr 20 '22 at 13:17
  • Yes, how can I do that? I want it to contain some dynamically changing text – Laura Apr 20 '22 at 13:19
  • 1
    Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/244052/discussion-between-laura-and-0stone0). – Laura Apr 20 '22 at 13:22
  • I've updated the code-snippet. `caption` is the non-bold variable text. Using `&caption=*{caption}*` in the url to make it bold. – 0stone0 Apr 20 '22 at 13:25