使用Scrapy在管道中发送通知时,电报响应400

nwlls2ji  于 2022-11-09  发布在  其他
关注(0)|答案(1)|浏览(146)

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>" + "\n\n" + \
                str(old) + " TL >>>> " + \
                str(new) + f" TL - <b>{percentage_str}%</b>" + "\n\n" + \
                url + "\n\n" + \
                "https://www.hepsiburada.com/" + "ara?q=" + productid + "\n\n"

            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]>

nnvyjq4y

nnvyjq4y1#

如果要发送包含htmlmarkdown标记的响应,则需要使用解析器传递它。
如果你使用html添加parse_mode=html
如果您正在使用markdown,请将parse_mode=markdown添加到URL
变更

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

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

message必须与URL兼容

import urllib
urllib.parse.quote(message)

相关问题