forked from ArtezGDA/Chatbots
-
Notifications
You must be signed in to change notification settings - Fork 0
/
chatServerModifyGlobals.py
75 lines (60 loc) · 1.83 KB
/
chatServerModifyGlobals.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#!/usr/bin/env python
# Command Line Interface (CLI) version
import os
import sys
import time
import chatSessionVariables as sessionVars
import rogerbot as bot
# Chat Server Framework functions
def sleep(n):
"""Sleep n number of seconds.
Pauses the execution of the program.
"""
time.sleep(n)
def output(s):
"""Outputs string s as chat message.
Send the given string to the chat client.
"""
print s
# Get the session from python introspection
def sessionFromIntrospection():
selfFile = os.path.splitext(os.path.basename(__file__))[0]
callDepth = 2
caller = None
try:
while True:
caller = sys._getframe(callDepth)
callerFullPath = caller.f_code.co_filename
callerFile = os.path.splitext(os.path.basename(callerFullPath))[0]
if callerFile == selfFile:
break
callDepth += 1
except ValueError:
return "Session-NotFound-ToDeep"
if caller:
session = caller.f_locals.get('session', "Session-NotFound-NoLocalVar")
return session
return "Session-NotFound-NoCaller"
# Run forever on the command line
def main():
"""docstring for main"""
# Test local session variable
session = "12345"
#
# Setup
# Pre-setup we're not interested in the bot's global variables.
bot.setup()
# After setup we need to capture the state of the global vars
sessionVars.storeGlobals(bot, session)
#
# Run continuesly
while True:
humanSpeak = raw_input("> ")
#
# Before responding, we need to inject the session state
sessionVars.injectGlobals(bot, session)
bot.response(humanSpeak)
# After responding, capture the global vars for the session state
sessionVars.storeGlobals(bot, session)
if __name__ == '__main__':
main()