-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrunbot.py
More file actions
71 lines (53 loc) · 1.56 KB
/
runbot.py
File metadata and controls
71 lines (53 loc) · 1.56 KB
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
import chatbot.loader
import sys, getopt
import traceback
def main(argv):
#
# Reads the arguments, in particular the name of the xml file where the bot definition is
# expected.
#
inputfile = None
try:
opts, args = getopt.getopt(argv,"hi:", ["ifile="])
except getopt.GetoptError:
usage()
sys.exit(2)
for opt, arg in opts:
if opt == '-i':
inputfile = arg
context = {}
for i in range(2,len(argv)):
name, var = argv[i].partition("=")[::2]
context[name] = var
if inputfile == None:
usage()
sys.exit(2)
#
# This is the main part. It shows how to use the current implementation to
# work in a synchronous way.
#
try:
bot = chatbot.loader.load(inputfile,context=context)
bot.hello = hello
while not bot.isFinished():
while not bot.isWaitingForInput():
message = bot.getNextMessage()
print(bot.getBotPrompt()+message)
userInput = input(bot.getUserPrompt())
bot.processInput(userInput)
while not bot.isWaitingForInput():
message = bot.getNextMessage()
if message == None: break
print(bot.getBotPrompt()+message)
except:
traceback.print_exc()
def hello(str):
print("Hello!")
return True
#
# A function that prints the usage using a terminal
#
def usage():
print('usage: chatbot.py -i <chatbotfile>')
if __name__ == "__main__":
main(sys.argv[1:])