Telegram обрывает соединение с ботом в режиме ожидания
Telegram просто обрывает соединение, если бот стоит в ожидании больше 5 минут. До 5 минут - все отлично работает! А нужно, чтобы бот постил один раз в час, например. При окончании ожидания в 3600 секунд выдает ошибку.
raise NetworkError(f'urllib3 HTTPError {error}') from error NetworkError: urllib3 HTTPError ('Connection aborted.', RemoteDisconnected('Remote end closed connection without response'))
Код, отвечающий за отправку сообщений в т.ч.:
num_new_posts = 0
while num_new_posts < 3:
headers = {
'User-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.121 Safari/537.36',
'Accept-Language': 'en-US,en;q=0.9',
'Referer': 'https://best.aliexpress.ru/',
}
# choose a random URL to parse photos from
url_list = ["https://example.com/"]
url = random.choice(url_list)
response = requests.get(url, headers=headers)
soup = BeautifulSoup(response.content, 'html.parser')
image_list = soup.findAll('img')
random.shuffle(image_list)
for img in image_list:
img_url = img.get('src')
if img_url and '/*****/' in img_url and img_url.startswith('http'):
# get the link and description of the image
link_tag = img.find_previous('a')
if link_tag:
link = link_tag.get('href')
description = link_tag.text
photo_id = img_url.split('/')[-1].split('.')[0] # use the photo ID as a unique identifier
if not check_recent_photos(photo_id):
# download the image
response = requests.get(img_url)
with open(f"data/{photo_id}.jpg", "wb") as f:
f.write(response.content)
# send the image to the Telegram channel
with open(f"data/{photo_id}.jpg", "rb") as f:
try:
bot.send_photo(chat_id=channel_id, photo=f, caption=description)
except telegram.error.BadRequest as e:
print(f"Failed to send photo {photo_id}:
finally:
f.close() {str(e)}")
# add the photo to the list of recently posted photos
add_recent_photo(photo_id)
break
sleep(3600)