简体   繁体   中英

How do I make a python bot with selenium that checks when a new message has arrived from a Telegram channel?

I should start off mentioning I am pretty new to python and selenium, so I don't really know much about either of them.

what I am trying to do:

  • Check every second if a telegram channel I follow has sent out a new message. I have telegram web opened on a browser opened with custom flags so that I don't need to log in with telegram every single time.

So far what I did is see the class that has all the message's text inside of it im_message_text , and get the last of them in the list.

Source Code

NOTE The "strong" tag is simply where the title is stored in side the text message, which is what i'm using to compare everything:

    def getItemsInfo1(self):
    try:
        iteminfo1 = WebDriverWait(self.driver, 4).until(
            EC.presence_of_element_located((By.CLASS_NAME, 'im_message_text')))
        list3 = self.driver.find_elements_by_class_name("im_message_text")
        list33 = list3[-1]
        list4 = list33.find_element_by_tag_name("strong")
        list4txt = list4.text
        return list4txt

    except:
        print("Nothing is found")

What I do from here is I make another identical code which has a time.sleep(1) at the start to give the message time to appear, and i compare the 2 using the following while loop:

        while Main.getItemsInfo1() == Main.getItemsInfo2(): 
        print("No new messages. Try number ", count)
        print(Main.getItemsInfo1(), Main.getItemsInfo2())
        count += 1

So far, whilst as soon as there is a new message the code output highlights that the 2 methods have different text, it still does not end the while loop and it continues to say that there aren't any new messages.

Does anyone know how to help?

You don't even need to use telegram web or selenuim either, let's make it much more easier:

from telethon import TelegramClient, events

client = TelegramClient(PHONE_NUMBER, API_ID, API_HASH)

@client.on(events.NewMessage("chanel name"))
def new_message_listener(event):
    new_message = event.message.message
    media_status = event.message.media
    ## DO what you like with new_messsage

Just remember to change the variables in the TelegramClient , You can receive API_ID, API_HASH from Telegram website.

They should be like this:

api_id = 1027347
api_hash = "c0e2cfefac982659a52da625b81e2a99"
while Main.getItemsInfo1() == Main.getItemsInfo2():  
print("No new messages. Try number ", count)
print(Main.getItemsInfo1(), Main.getItemsInfo2())
count += 1

When you call getItemsInfo1() you are calling the method again, so there is no guarantee that the value is get in print statement is same as what is there in while so use something like:

info1= Main.getItemsInfo1()
info2= Main.getItemsInfo2()
while  info == Info2:  
   print("No new messages. Try number ", count)
   info1= Main.getItemsInfo1()
   info2= Main.getItemsInfo2()
   print(info1,info2)
   count += 1

The above code will make sure you are having same value, as you are storing the value to a variable and then validating it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM