Forums

how run few apps bots with flask on pythonanywhere

i made viber bot on python. its working fine. now i am trying to run few bots on one webserver. the first bot is working but the second one not.

app = Flask(__name__)


app2 = Flask('app2')
@app.route('/', methods=['POST'])
def incoming():
    viber_request = viber.parse_request(request.get_data())
    if isinstance(viber_request, ViberMessageRequest):
         viber.send_messages(viber_request.sender.id,[TextMessage(text='test1')] )

@app2.route('/ff', methods=['POST'])
def incoming2():
    viber_request = viber.parse_request(request.get_data())
    if isinstance(viber_request, ViberMessageRequest):
         viber.send_messages(viber_request.sender.id,[TextMessage(text='test2')] )

def set_webhook(viber):
    viber.unset_webhook()
    time.sleep(1)
    viber.set_webhook('https://myhost.pythonanywhere.com')

def set_webhook2(viber2):
    viber2.unset_webhook()
    time.sleep(1)
    viber2.set_webhook('https://myhost.pythonanywhere.com/ff')



if __name__ == "__main__":
    scheduler = sched.scheduler(time.time, time.sleep)
    scheduler.enter(5, 1, set_webhook, (viber,))
    t = threading.Thread(target=scheduler.run)
    t.start()

    scheduler = sched.scheduler(time.time, time.sleep)
    scheduler.enter(5, 1, set_webhook2, (viber2,))
    t = threading.Thread(target=scheduler.run)
    t.start()


    app.run(host='https://myhost.pythonanywhere.com', port=8443, debug=True)
    app2.run(host='https://myhost.pythonanywhere.com/ff', port=8080, debug=True)

first bot responding with test1 but second one silent. how to run few bots on one webserver properly ? Both tokens are correct

I'm actually surprised that either one is working -- code that is inside an if __name__ == "__main__" block won't be run in a production uWSGI environment like PythonAnywhere. Perhaps you set the webhook for the first bot somewhere outside the web environment, for example from a console?

Anyway -- is there a particular reason why you're using the scheduler to call set_webhook and set_webhook2? You should just be able to call them at module level, eg. put calls like

set_webhook()
set_webhook2()

...outside the if __name__... statement.

thanks for reply! you right it was working from console now i made :

def set_webhook(viber):
    viber.unset_webhook()
    time.sleep(1)
    viber.set_webhook('https://admiralf.pythonanywhere.com')

def set_webhook2(viber2):
    viber.unset_webhook()
    time.sleep(1)
    viber2.set_webhook('https://admiralf.pythonanywhere.com/f')


set_webhook(viber)
set_webhook2(viber2)
if __name__ == "__main__":


    app.run(host='https://myhost.pythonanywhere.com', port=8443, debug=True)#, ssl_context=context)
    app2.run(host='https://myhost.pythonanywhere.com/f', port=8080, debug=True)#, ssl_context=context)

bot bots not working now i get the following error :

2019-01-01 14:03:51,119: Exception: failed with status: 1, message: Result[HttpRequest[POST / HTTP/1.1]@5227d8aa > HttpResponse[null 0 null]@25e2cbc5] java.util.concurrent.TimeoutException: Total timeout elapsed
2019-01-01 14:03:51,120:   File "/var/www/admiralf_pythonanywhere_com_wsgi.py", line 16, in <module>
2019-01-01 14:03:51,120:     from flask_app import app as application  # noqa
2019-01-01 14:03:51,120: 
2019-01-01 14:03:51,121:   File "/home/admiralf/mysite/flask_app.py", line 448, in <module>
2019-01-01 14:03:51,121:     set_webhook(viber)
2019-01-01 14:03:51,121: 
2019-01-01 14:03:51,122:   File "/home/admiralf/mysite/flask_app.py", line 189, in set_webhook
2019-01-01 14:03:51,122:     viber.set_webhook('https://admiralf.pythonanywhere.com')
2019-01-01 14:03:51,122: 
2019-01-01 14:03:51,123:   File "/home/admiralf/.local/lib/python3.7/site-packages/viberbot/api/api.py", line 29, in set_webhook
2019-01-01 14:03:51,123:     return self._request_sender.set_webhook(url, webhook_events, is_inline)
2019-01-01 14:03:51,123: 
2019-01-01 14:03:51,124:   File "/home/admiralf/.local/lib/python3.7/site-packages/viberbot/api/api_request_sender.py", line 32, in set_webhook
2019-01-01 14:03:51,124:     raise Exception(u"failed with status: {0}, message: {1}".format(result['status'], result['status_message']))

@ giles , thanks for reply! you right it was working from console now i made :

def set_webhook(viber):
    viber.unset_webhook()
    time.sleep(1)
    viber.set_webhook('https://admiralf.pythonanywhere.com')

def set_webhook2(viber2):
    viber.unset_webhook()
    time.sleep(1)
    viber2.set_webhook('https://admiralf.pythonanywhere.com/f')


set_webhook(viber)
set_webhook2(viber2)
if __name__ == "__main__":


    app.run(host='https://myhost.pythonanywhere.com', port=8443, debug=True)#, ssl_context=context)
    app2.run(host='https://myhost.pythonanywhere.com/f', port=8080, debug=True)#, ssl_context=context)

bot bots not working now i get the following error :

2019-01-01 14:03:51,119: Exception: failed with status: 1, message: Result[HttpRequest[POST / HTTP/1.1]@5227d8aa > HttpResponse[null 0 null]@25e2cbc5] java.util.concurrent.TimeoutException: Total timeout elapsed
2019-01-01 14:03:51,120:   File "/var/www/admiralf_pythonanywhere_com_wsgi.py", line 16, in <module>
2019-01-01 14:03:51,120:     from flask_app import app as application  # noqa
2019-01-01 14:03:51,120: 
2019-01-01 14:03:51,121:   File "/home/admiralf/mysite/flask_app.py", line 448, in <module>
2019-01-01 14:03:51,121:     set_webhook(viber)
2019-01-01 14:03:51,121: 
2019-01-01 14:03:51,122:   File "/home/admiralf/mysite/flask_app.py", line 189, in set_webhook
2019-01-01 14:03:51,122:     viber.set_webhook('https://admiralf.pythonanywhere.com')
2019-01-01 14:03:51,122: 
2019-01-01 14:03:51,123:   File "/home/admiralf/.local/lib/python3.7/site-packages/viberbot/api/api.py", line 29, in set_webhook
2019-01-01 14:03:51,123:     return self._request_sender.set_webhook(url, webhook_events, is_inline)
2019-01-01 14:03:51,123: 
2019-01-01 14:03:51,124:   File "/home/admiralf/.local/lib/python3.7/site-packages/viberbot/api/api_request_sender.py", line 32, in set_webhook
2019-01-01 14:03:51,124:     raise Exception(u"failed with status: {0}, message: {1}".format(result['status'], result['status_message']))

@ giles , thanks for reply! you right it was working from console now i made :

def set_webhook(viber):
    viber.unset_webhook()
    time.sleep(1)
    viber.set_webhook('https://admiralf.pythonanywhere.com')

def set_webhook2(viber2):
    viber.unset_webhook()
    time.sleep(1)
    viber2.set_webhook('https://admiralf.pythonanywhere.com/f')


set_webhook(viber)
set_webhook2(viber2)
if __name__ == "__main__":


    app.run(host='https://myhost.pythonanywhere.com', port=8443, debug=True)#, ssl_context=context)
    app2.run(host='https://myhost.pythonanywhere.com/f', port=8080, debug=True)#, ssl_context=context)

bot bots not working now i get the following error :

2019-01-01 14:03:51,119: Exception: failed with status: 1, message: Result[HttpRequest[POST / HTTP/1.1]@5227d8aa > HttpResponse[null 0 null]@25e2cbc5] java.util.concurrent.TimeoutException: Total timeout elapsed
2019-01-01 14:03:51,120:   File "/var/www/admiralf_pythonanywhere_com_wsgi.py", line 16, in <module>
2019-01-01 14:03:51,120:     from flask_app import app as application  # noqa
2019-01-01 14:03:51,120: 
2019-01-01 14:03:51,121:   File "/home/admiralf/mysite/flask_app.py", line 448, in <module>
2019-01-01 14:03:51,121:     set_webhook(viber)
2019-01-01 14:03:51,121: 
2019-01-01 14:03:51,122:   File "/home/admiralf/mysite/flask_app.py", line 189, in set_webhook
2019-01-01 14:03:51,122:     viber.set_webhook('https://admiralf.pythonanywhere.com')
2019-01-01 14:03:51,122: 
2019-01-01 14:03:51,123:   File "/home/admiralf/.local/lib/python3.7/site-packages/viberbot/api/api.py", line 29, in set_webhook
2019-01-01 14:03:51,123:     return self._request_sender.set_webhook(url, webhook_events, is_inline)
2019-01-01 14:03:51,123: 
2019-01-01 14:03:51,124:   File "/home/admiralf/.local/lib/python3.7/site-packages/viberbot/api/api_request_sender.py", line 32, in set_webhook
2019-01-01 14:03:51,124:     raise Exception(u"failed with status: {0}, message: {1}".format(result['status'], result['status_message']))

what is viber and viber2?

@conrad

viber2 = Api(BotConfiguration(
  name='PythonSampleBot1',
  avatar='https://pp.userapi.com/c849424/v849424201/e62ce/b4gUG562vZw.jpg',
  auth_token='43443****
))


viber = Api(BotConfiguration(
    name='Bot',
    avatar='https://pp.userapi.com/c849424/v849424201/e62ce/b4gUG562vZw.jpg',
    auth_token="43****"))

both tokens are correct

It really looks to me like you're getting a timeout from viber when you're setting up the webhook. I don't know enough about their service to know why that might be. Try contacting their support.

@glenn the strange thing is that first bot is working when I am running the console . so if that was connected with Viber server somehow I guess first bot should not work either .

All I have to go on is what you are posting and that shows a timeout exception. Though I have noticed that in set_webhook2, you are unsetting a webhook on viber and not viber2.

i tried all the variations with set and unset

i have no idea what to do else (( @glenn anyway thank you for your help

I think Glenn is right, you should probably contact Viber support and find out why it's timing out when it tries to connect to their servers. One thing that comes to mind -- maybe if you have one copy of your code running in a console and one in side a website that you're set up on the "Web" tab, they might interfere with each other? To at least eliminate that possibility, you can make sure that you're not running it in a console (you can use the "Running processes" page on the "Consoles" page to do that) and then reload the website from the "Web" tab to see if it works then.

Oh, and one thing I just spotted -- the timeout says that it's coming from Java, so that would definitely be something coming from the Viber side -- we don't support Java in website code, so the message must be coming from some Java code running on Viber's servers.

I think that pythonanywhere wrote "java.util.concurrent.TimeoutException: Total timeout elapsed" because in a FREE account uses ONLY one thread: this script can not send a request and confirm it at the same time.

That's not quite correct. You cannot start threads in a web app at all, the Freeness of the account done not matter.

don't work at all! viber cannot make app.run with any data like as app.run(url,port,debug). https://help.pythonanywhere.com/pages/Flask/ only as app.run()

What is not working for you, @RomanLisitsin?