简体   繁体   中英

Deploy a simple Python bot

Code:

    import requests as rq
    from bs4 import BeautifulSoup as bs

    url = "https://apod.nasa.gov/apod/astropix.html"
    page = rq.get(url).content
    soup = bs(page, 'html.parser')
    response = soup.find('img')
    if response == None:
        imglink = soup.find('iframe')['src']
    else:
        imglink = 'https://apod.nasa.gov/apod/' + response['src']
    def main():
        sess = rq.Session()
        cid='**************'
        turl = 'https://api.telegram.org/bot*******************/'
        if response == None:
            imglink = soup.find('iframe')['src']
            params = {'chat_id':cid,'text':imglink}
            sess.post(turl + 'sendMessage', data=params)
        else:
            imglink = 'https://apod.nasa.gov/apod/' + response['src']
            title = soup.find('b').get_text()
            params = {'chat_id':cid,'photo':imglink,'caption':title}
           sess.post(turl + 'sendPhoto', data=params)

    if __name__ == '__main__':
        main()

This is a simple bot for sending Nasa picture to my telegram channel. I will be modifying this script to make it happen everyday. But he Question is where do I host them, So that it will run all the time (free). What is the correct way of doing it.

I don't know of any providers that would host this for free. For cheap, AWS and Google Cloud all have simple solutions.

Ex: https://cloud.google.com/blog/products/application-development/how-to-schedule-a-recurring-python-script-on-gcp

See if https://www.heroku.com free tier would meet your needs.

  1. If so create a git repository in your directory with:
git init
  1. In your directory along with your.py script create a file called Procfile , and in it put:
worker: python your_script_name_here.py
  1. At https://dashboard.heroku.com/apps/ create a new app (top right corner).
  2. Set up git remote to point to Heroku: https://devcenter.heroku.com/articles/git#creating-a-heroku-remote
  3. Push changes from your local repository to Heroku with:
git push origin heroku 
  1. Configure the Heroku Scheduler add-on.
  2. Schedule your job to run every 10 minutes, every hour, or once a day.

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