Forums

website-change-telegram

hello, I'm trying to run telegram bot. gethub code: https://github.com/Wunders/website-change-telegram/blob/master/website-change-telegram.py

# UrlChange
# Checks for an updates to a given list of urls, and reports changes

# Tested with Python version 3.7.6

import requests
import re
import os
import schedule
import time

# Send a message via a telegram bot
def telegram_bot_sendtext(bot_message):
    bot_token = 'YOUR_BOT_TOKEN'
    bot_chatID = 'TELEGRAM_USER_CHAT_ID'
    send_text = 'https://api.telegram.org/bot' + bot_token + '/sendMessage?chat_id=' + bot_chatID + '&parse_mode=Markdown&text=' + bot_message

    response = requests.get(send_text)

    return response.json()

def report_change(url):
    html_response = str(requests.get(url))
    file_name = ''.join(x for x in url if x.isalpha()) + ".txt"

    # Check if file exists that matches the page's content
    if os.path.exists(file_name):
        cache_file = open(file_name, "r")
        html_cache = cache_file.read()
        # If the updated set is not equal to the stored set, update and report change
        if html_response != html_cache:
            cache_file = open(file_name, "w")
            cache_file.write(html_response)
            print("website change reported!")
            # Send the message (such as with a telegram bot provided below)
            telegram_bot_sendtext("Website change detected at url: " + url)
        else:
            print("no change")
    else:
        # save the url's html content to a file
        print("no cache file for " + url + " found, creating one...")
        cache_file = open(file_name, "w")
        cache_file.write(html_response)

# Read liust of urls from file
def scan_url():
    # Get list of urls from file:
    with open("urls.txt") as urls_file:
        urls_list = urls_file.readlines()
    urls_list = [x.strip() for x in urls_list]
    # Check each url for changes
    for url in urls_list:
        report_change(url)
        time.sleep(1)

# Initalize script to run every 2 minutes
scan_url()
schedule.every(2).seconds.do(scan_url)
while True:
    schedule.run_pending()
    time.sleep(1)

but PythonAnywhere bash console gives me No module named 'schedule'. can you help me to solve?

Could you share the error?

(mysite-virtualenv) 12:42 ~ $ ls README.txt website-change-telegram.py (mysite-virtualenv) 12:42 ~ $ python website-change-telegram.py Traceback (most recent call last): File "website-change-telegram.py", line 9, in <module> import schedule ImportError: No module named 'schedule' (mysite-virtualenv) 12:44 ~ $ python website-change-telegram.py Traceback (most recent call last): File "website-change-telegram.py", line 9, in <module> import schedule ImportError: No module named 'schedule' (mysite-virtualenv) 12:46 ~ $

You're missing the schedule module. That is either because you have not installed the package that provides it, or because it's code from your project that is either not there or not on your Python path. See our help page on how to debug errors like that: https://help.pythonanywhere.com/pages/DebuggingImportError/

the procedure I use is: $ mkvirtualenv --python=/usr/bin/python3 mysite-virtualenv

$ pip install pyTelegramBotAPI

$ python website-change-telegram.py

what is wrong?

Is the schedule module included in the pyTelegramBotAPI package? If it's not, then you will need to install whatever package provides it.