Код:
import requests
import re
BOT_TOKEN = 'YOUR_BOT_TOKEN'
SOURCE_CHANNEL_ID = 'SOURCE_CHANNEL_ID'
TARGET_CHANNEL_ID = 'TARGET_CHANNEL_ID'
def send_message(chat_id, text):
url = f'https://api.telegram.org/bot{BOT_TOKEN}/sendMessage'
params = {'chat_id': chat_id, 'text': text}
requests.post(url, json=params)
def send_photo(chat_id, photo):
url = f'https://api.telegram.org/bot{BOT_TOKEN}/sendPhoto'
params = {'chat_id': chat_id}
files = {'photo': photo}
requests.post(url, params=params, files=files)
def handle_new_posts(message):
lines = message.split('\n')
name = lines[0] # Первая строка
for line in lines:
if "Дроп" in line:
price_match = re.search(r'\bДроп\s*(\d+)\b', line)
if price_match:
price = int(price_match.group(1)) + 1000
price_str = f"{price} RUB"
break
else:
return # Если нет строки с "Дроп", выходим
new_post = f"{name}\nКачество: Luxe\nРазмеры в наличии\n{price_str}\nВсе фото живые и соответствуют товару\nОтправка по всей России"
send_message(TARGET_CHANNEL_ID, new_post)
if 'photo' in message:
for photo in message['photo']:
send_photo(TARGET_CHANNEL_ID, photo['file_id'])
def main():
offset = None
while True:
response = requests.get(f'https://api.telegram.org/bot{BOT_TOKEN}/getUpdates', params={'offset': offset, 'timeout': 30})
if response.status_code == 200:
updates = response.json().get('result')
if updates:
for update in updates:
offset = update['update_id'] + 1
if 'message' in update and 'text' in update['message']:
if update['message']['chat']['type'] == 'channel' and str(update['message']['chat']['id']) == SOURCE_CHANNEL_ID:
handle_new_posts(update['message']['text'])
if __name__ == '__main__':
main()
помогите пожалуйста