'Python telegram bot mulitple use and runtime problems
So I'm trying for the first time to use Telegram Bot and Python, I want it will let me know the stats for a player I follow in the NBA after each game automatically and for other users who wish to use the bot. This is my code:
import urllib.request
import os
from urllib.request import urlopen
from bs4 import BeautifulSoup
from telegram import *
from telegram.ext import *
import time
TOKEN = "MY TOKEN HERE"
bot = Bot(TOKEN)
PORT = int(os.environ.get('PORT', 5000))
def start(update: Update, context: CallbackContext):
DeniList=[]
Date=1/1/2020
count=0
StatPage = urlopen("MY URL")
SoupPage = BeautifulSoup(StatPage.read(),"html.parser")
while True:
for tbody in SoupPage.find_all("tbody"):
for td in tbody.find_all("td"):
if (count==2):
break
if (Date==td.text):
break
DeniList.append(td.text)
count+=1
if (count==2):
Date=DeniList[0]
Points=DeniList[2]
context.bot.send_message(text=Date + Points)
count=0
time.sleep(1600)
def main():
updater=Updater(TOKEN ,use_context=True)
dp=updater.dispatcher
start_handler=CommandHandler('start',start)
dp.add_handler(start_handler)
updater.start_webhook(listen="0.0.0.0",
port=int(PORT),
url_path=TOKEN, webhook_url='https://denibotnba.herokuapp.com/' + TOKEN)
updater.idle()
if __name__=='__main__':
main()
This code is checking around every 30 mins if a new game is uploaded to the site, and tells me the new stats if so. BUT, I have 2 problems:
- This code is now live on Heroku. Whenever I press /start on my telegram, for the first time it will tell me the latest game. But after, if myself (from my user again) or someone else from another telegram user is pressing /start it will not show anything for anyone. It will only work for the first time someone pressed /start from any telegram user and it affects all users of the bot altogether.
- after less than an hour on Heroku, the log shows me "Error R12 (Exit timeout) -> At least one process failed to exit within 30 seconds of SIGTERM" and stops the code. why? What can I do?
Thanks! I'm a newbie, I know :)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
