Home » Javascript » Python – Bots, Asyncio, Javascript, and Loops

Python – Bots, Asyncio, Javascript, and Loops

Posted by: admin August 19, 2018 Leave a comment


Hello Everyone and thank you for your time. In the previous couple of days I have been undergoing a little project of mine to create a online chatbot for LINE. I understand the API but ran into a problem. I am trying to get my chabot to use online responses to user text. To accomplish this I am trying to use a websocket between python and JavaScript. The API I am using is in python hence the websocket must also be written in Python. I picked asyncio websockets. In short, I am trying to fetch a response from a user input on Line –> Send that response over a websocket to my javascript in a browser console –> The javascript will proceed with it’s logic and send the response back to the server –> the Program will proceed with it’s execution. My problem is that both the Websocket programming as well as my Line programming use a loop. The Line Code can be seen at


As you can see at the end of the code there is a while statement which traces the READ_MESSAGE Function over and over again for each message. You can ignore the NOTIFIED_LEAVE one. So basically my code while proceed with it’s own logic and then do something like:

#ExternalBot code
ExtText = text[6:]

I now wish to send this ExtText over my websocket to my javascript which will fetch a response from the browser and send it back. The javascript will do something like this:

exampleSocket = new WebSocket("ws://") 
exampleSocket.onmessage = function(e){
var server_message = e.data;

Now this is where the problem begins as I simply do not know how to proceed. The python asyncio Websockets requires a loop to run as seen below:

async def Send(websocket, path):
    await websocket.send(ExtText)
    await Receive(websocket, path)

async def Receive(websocket, path):
    resposne = await websocket.recv()
    if response is None:
        await Recieve(websocket, path)
        print (Response)

    start_server = websockets.serve(Send,'','8765')
    loop = asyncio.get_event_loop()

   (continue to do a line.send() the response back to the client on Line.)

This is the code that I currently have. Now the problem is, If I never close the loop the loop with something like loop.call_soon_threadsafe(loop.stop) it will keep running and in turn the message will never get sent back to the client(the program will not proceed with it’s LINE logic). if I DO close the loop the server is “shutdown” in effect. If the server is shutdown and reopened for each message I have to send over the websocket, I now face the problem of getting my JavaScript to continually try to reconnect to the server every single message. I have tried this and have never succeeded in getting the Javascript to keep trying to establish a connection. I have tried to use Javascript websocket readyState attribute but that hasn’t worked either. I am coming here simply because I Have no idea on how to proceed and what to do further. How do you people suggest I proceed, closing the loop or somehow leaving it open but continuing with my program’s logic. If so how would you go about implementing it into my already looping code( the LINE while true statement)? I really do apologize for such a long question but I am completely stuck. Thank you for your time people who decide to read and help me, I really appreciate it.