如何使用带python selenium的电报机器人发送消息

2024-04-18 18:28:09 发布

您现在位置:Python中文网/ 问答频道 /正文

我想构建一个脚本,如果页面上的元素不存在,它可以通过bot电报向我发送消息。我尝试过以下代码:

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains
import time
import telegram

driver = webdriver.Chrome()
driver.get("https://site123123.ma")
#login
username_box = driver.find_element_by_xpath('//*[@id="user_email"]')
username_box.send_keys('email')
password_box = driver.find_element_by_xpath('//*[@id="user_password"]')
password_box.send_keys('Password')
submit_button = driver.find_element_by_xpath('//*[@id="new_user"]/div[2]/div[3]/input')
submit_button.click()
time.sleep(7)
#check the element if non exist send me message via bot telegram : help me here
#if the element is exist try every 1 min until the elemnt is non exist : help me here
notice=driver.find_element_by_xpath('//*[@id="subs-content"]/p') 
#my token
bot=telegram.Bot('14XXXXX751:AAEY-XXXXXX-70ADVkuHawry-GO28Fk')
#my chat id :help me here
bot.send_message('9XXXXXX13', 'The POOL is ready You can Now take your place Fast')

我的代码遗漏了很多东西,只需查看代码中的注释即可


Tags: 代码fromimportboxsendidbybot
1条回答
网友
1楼 · 发布于 2024-04-18 18:28:09

步骤1:在电报上搜索bot,制作一个简单的bot并复制bot令牌,参考:https://medium.com/@ManHay_Hong/how-to-create-a-telegram-bot-and-send-messages-with-python-4cf314d9fa3e

步骤2:将机器人添加到要发送通知的频道

第三步:点击https://api.telegram.org/bot<YOUR TOKEN>/getUpdates你将获得相应频道的聊天id

步骤4:使用此代码

import requests
bot_token = ''
bot_chatID = ''
bot_message = 'test'
send_api = 'https://api.telegram.org/bot' + bot_token + '/sendMessage?chat_id=' + bot_chatID + '&parse_mode=Markdown&text=' + bot_message
response = requests.get(send_api)

相关问题 更多 >