问题描述
所以我不知道如何正确地提出这个问题,所以它可能看起来有点不对劲,抱歉。
我制作了一个电报机器人,可以从网站获取一些图像并将其发送到您的聊天室。但是,当用户调用该命令时,照片也会发送给已启动机器人的其他用户。
例如,如果用户 A 调用命令来获取照片,机器人会将其发送给他以及用户 B、用户 C 和用户 D,所有这些都堆叠在一起,就好像这是对每个使用机器人
import requests
import os
from tqdm import tqdm
from bs4 import BeautifulSoup as bs
from urllib.parse import urljoin,urlparse
import re
import telebot
API_KEY = os.getenv("API_KEY")
bot = telebot.TeleBot(API_KEY)
url_mainpage = "https://url.com"
soup = bs(requests.get(url_mainpage).content,"html.parser")
full_link = soup.find("h5",class_="elementor-image-Box-title")
selectlist = full_link.select(".elementor-image-Box-title a")
for a in selectlist:
global lastchapterlink
lastchapterlink = a['href']
images = []
stripped_images = []
def download_last_chapter():
soup = bs(requests.get(lastchapterlink).content,"html.parser")
images_link = soup.findAll("img")
for img in images_link:
images.append(img.get("src"))
for link in images:
stripped_images.append(link.strip())
print(stripped_images)
@bot.message_handler(commands=["last_chapter"])
def send_images(message):
download_last_chapter()
for telegram_send in stripped_images:
try:
bot.send_photo(message.chat.id,photo = telegram_send)
except:
None
bot.polling()
这是包含机器人的代码部分
解决方法
根据 API documentation,机器人将在它看到消息的任何频道中回复。您的用户是在发送消息,还是在您参与的共享频道中发帖?此外,您不会在调用之间清除 stripped_images - 您只是将新图像附加到其中。