簡體   English   中英

如何使用帶有 python selenium 的電報機器人發送消息

[英]How I can send message using telegram bot with python selenium

如果頁面上的元素不存在,我想構建一個腳本,該腳本可以通過機器人電報向我發送消息。 我試過這段代碼:

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')

我的代碼漏掉了很多東西,看代碼中的注釋即可。

step1:在電報上搜索botfather,制作一個簡單的bot並復制bot令牌,參考: https://medium.com/@ManHay_Hong/how-to-create-a-telegram-bot-and-send-messages-with- python-4cf314d9fa3e

step2:將機器人添加到您要發送通知的頻道

第三步:點擊https://api.telegram.org/bot<YOUR TOKEN>/getUpdates你將獲得相應頻道的 chat_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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM