Forums

web.py chatbot using NLTK

Hey guys, I need your advice :) I am trying to deploy a chatbot application. <p>the html part works - the website is running </p> <p>however, i cannot submit any text in the chatwindow </p> <p>it works locally but not on pythonanywhere and I really dont know why </p> <p>I am using web.py for the chatwindow and ajax for updatting it </p> <p> those are my post and get

<pre>
messages = []  
thread_lock = {}
session_pos = {}
counter = 0

urls = (
'/', 'Frame',
'/longpoll/([0-9]+)', 'LongPoll',
'/readall', 'ReadAll',
'/send', 'Say',
'/stop', 'Stop',
)
class LongPoll:
def GET(self, session_id):
    webpy.header('Content-type', 'text/html')
    thread_id = str(threading.current_thread())
    if session_id not in session_pos:
        session_pos[session_id] = 0
    if session_pos[session_id] == len(messages):
        thread_lock[thread_id] = threading.Event()
        thread_lock[thread_id].clear()
        thread_lock[thread_id].wait()
    while session_pos[session_id] < len(messages):
        msg = messages[session_pos[session_id]]
        yield '<div>%s</div>\n' % msg
        session_pos[session_id] += 1

class ReadAll:
def GET(self):
    webpy.header('Content-type', 'text/html')
    thread_id = str(threading.current_thread())
    pos = 0
    while True:
        if pos == len(messages):
            thread_lock[thread_id] = threading.Event()
            thread_lock[thread_id].clear()
            thread_lock[thread_id].wait()
        yield messages[pos] + '\n'
        pos += 1

class Say:
def POST(self):
    bot = "BOT: \n"
    line = webpy.input()['l']
    #print "MESSAGES", messages
    response = test_function(line)        
    response = bot + response
    messages.append(line)
    messages.append(response)
    for thread in thread_lock:
        thread_lock[thread].set()
    #print "COUNTER: ", counter
    return response

class Stop:
def GET(self):
    os._exit(0)

class Frame:
def GET(self):
    input = webpy.input()
    if 'l' in input:
        line = input['l']
        messages.append(line)
        for thread in thread_lock:
            thread_lock[thread].set()
    randnum = random.randint(0, 2000000000)
     </pre>

and this is the html page with javascript <pre> page = """<html>

<script type="text/javascript">

    $('#text').keypress(function(event) {
    if (event.keyCode == '13')
         sendMsg();
    });

    function sendMsg() {
        var text = $('#text');
        var msg = text.val();
        $.post('/send', {'l': msg});
        text.val('');
    }

    function stop() {
        $.ajax({url: '/stop'});
        $('#chat').append('&nbsp;&nbsp;&nbsp;&nbsp;*** Stop. You may close the window. ***');
    }

    function getMsg() {
        $.ajax({
            url: '/longpoll/%d',
            dataType: 'text',
            type: 'get',
            success: function(line) {
                var you = "YOU: ";
                var ymsg = you.concat(line.trim());

                $('#chat').append(ymsg.trim(9));
                $("#chat").attr({ scrollTop: $("#chat").attr("scrollHeight") });
                setTimeout('getMsg()', 100);

             }
        });
    }
    getMsg();
</script>

</body>




<html>"""
    return page % randnum

</pre>

does anybody have an idea why i cannot post anything in the chatwindow so that the submitted part is analysed by some ntlk functions defined in function.py? <p> thanks!

Check in your browser's console and network log to see what is happening when you send your chat message.

it doesnt even let me send the message. only the hardcoded stuff of the html works. how do I check those logs? i think maybe its something with the port. Ive read that pythonanywhere allows to use only certain ports and I am using 8080

That might be the problem; you can only access your PythonAnywhere web app on ports 80 (for normal HTTP) and 443 (for HTTPS). If you switch back to one of those then it should work fine.

Ok i changed the code so that it uses port 80 but it gives me a socket error now and says i cannot create the port :( any idea why?

Hi there,

On the server-side, we take care of running your application and choosing what port it's on, so you shouldn't need to supply any kind of port, 8000 or 80 or any of those. Just import a wsgi-compatible application module into your wsgi file.

The important thing is that your code shouldn't try to actually "run" a server itself. I don't know much about web.py, but in Flask there's a thing called app.run(), which you should not use on PythonAnywhere. more info here