Telegram response 400 when I sent notification in pipeline using Scrapy

Question:

I tried sending notification using telegram bot but I got response 400.

My code (in pipeline):

    def sendnotifications(self, token):
        cursor = self.cnx.cursor()
        req = requests
        cursor.execute("SELECT * FROM notificate WHERE token= '"+token+"'")
        notifications = cursor.fetchall()
        for notification in notifications:
            print(notification)
            productid = notification[1]
            url = notification[3]
            name = notification[2]
            old = notification[4]
            new = notification[5]
            price_difference = old - new
            percentage = price_difference / old
            percentage_str = str("%.2f" % (percentage * 100))

            message = "<b>" + name + "</b>" + "nn" + 
                str(old) + " TL >>>> " + 
                str(new) + f" TL - <b>{percentage_str}%</b>" + "nn" + 
                url + "nn" + 
                "https://www.hepsiburada.com/" + "ara?q=" + productid + "nn"

            TOKEN = "xxxxxxxxxxxxxxxxxxxxx"
            chat_id = "xxxxxxxxxxxxx"
            tel_url = f"https://api.telegram.org/bot{TOKEN}/sendMessage?chat_id={chat_id}&text={message}"
            
            try:
                r = requests.get(tel_url
                )
                print(r)
            except RequestException:
                return False
        cursor.close()
        return True

I guess I make mistake but I can’t find any solutions.

Thanks for your helps.

I got this response:

(137, ‘hbv00000s3cb8’, ‘Asus Dual GeForce RTX 2060 EVO 6GB 192Bit GDDR6 (DX12) PCI-E 3.0 Ekran Kartı (DUAL-RTX2060-6G-EVO)’, ‘https://www.hepsiburada.com/asus-dual-geforce-rtx-2060-evo-6gb-192bit-gddr6-dx12-pci-e-3-0-ekran-karti-dual-rtx2060-6g-evo-p-HBV00000S3CB8’, Decimal(‘69000.00’), Decimal(‘10269.00’), datetime.datetime(2022, 9, 23, 11, 26, 51), ‘zoagxalqgm’)
2022-09-23 11:26:51 [urllib3.connectionpool] DEBUG: Starting new HTTPS connection (1): api.telegram.org:443
2022-09-23 11:26:52 [urllib3.connectionpool] DEBUG: https://api.telegram.org:443 "GET /bot5713682726:AAG3qGNfpGz9t_WyOVrTWaeB6sXf3NCFUIc/sendMessage?chat_id=1776233832&text=%3Cb%3EAsus%20Dual%20GeForce%20RTX%202060%20EVO%206GB%20192Bit%20GDDR6%20(DX12)%20PCI-E%203.0%20Ekran%20Kart%C4%B1%20(DUAL-RTX2060-6G-EVO)%3C/b%3E%0A%0A69000.00%20TL%20%3E%3E%3E%3E%2010269.00%20TL%20-%20%3Cb%3E85.12%25%3C/b%3E%0A%0Ahttps://www.hepsiburada.com/asus-dual-geforce-rtx-2060-evo-6gb-192bit-gddr6-dx12-pci-e-3-0-ekran-karti-dual-rtx2060-6g-evo-p-HBV00000S3CB8%0A%0Ahttps://www.hepsiburada.com/ara?q=hbv00000s3cb8%0A%0A HTTP/1.1" 400 73
<Response [400]>

Asked By: cevreyolu

||

Answers:

if you want to send something that contains html or markdown tags as response, you need to pass it with parsers.

if you are using html add parse_mode=html

if you are using markdown add parse_mode=markdown to the URL

change

tel_url = f"https://api.telegram.org/bot{TOKEN}/sendMessage?chat_id={chat_id}&text={message}"         

to

tel_url = f"https://api.telegram.org/bot{TOKEN}/sendMessage?chat_id={chat_id}&text={message}&parse_mode=html"

message need to be URL compatible

do

import urllib
urllib.parse.quote(message)
Answered By: iamtrappedman