-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgo-app.js
127 lines (104 loc) · 3.78 KB
/
go-app.js
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
var go = {};
go;
go.app = function() {
var vumigo = require('vumigo_v02');
var App = vumigo.App;
var Choice = vumigo.states.Choice;
var ChoiceState = vumigo.states.ChoiceState;
var EndState = vumigo.states.EndState;
var JsonApi = vumigo.http.api.JsonApi;
var FreeText = vumigo.states.FreeText;
var LanguageChoice = vumigo.states.LanguageChoice;
//var User = vumigo.user;
var GoApp = App.extend(function(self) {
App.call(self, 'states:start');
var $ = self.$;
self.init = function() {
self.http = new JsonApi(self.im);
//self.im.user.set_lang('af');
return self.im
.contacts.for_user()
.then(function(user_contact) {
self.contact = user_contact;
});
};
self.states.add('states:start', function(name) {
return self.contact.extra.registered === 'true'
? self.states.create('states:registered')
: self.states.create('states:language');
});
self.states.add('states:language', function(name) {
return new LanguageChoice(name,
{
next: "states:registration:name",
question: "What language would you like to use?",
choices: [
new Choice("af", "Afrikaans"),
new Choice("en", "English") ]
}
);
});
self.states.add('states:registration:name', function(name) {
return new FreeText(name, {
question: $('What is your name?'),
next: function(content) {
self.contact.name = content;
self.contact.extra.registered = 'true';
return self.im
.contacts.save(self.contact)
.then(function() {
return 'states:registered';
});
}
});
});
self.states.add('states:registered', function(name) {
return new ChoiceState(name, {
question: 'Hi ' + self.contact.name + '! What do you want to do?',
choices: [
new Choice('joke', 'Show me a joke'),
new Choice('end', 'Exit')],
next: function(choice) {
if (choice.value == 'joke') {
return self
.http.get('http://api.icndb.com/jokes/random?escape=javascript')
.then(function(resp){
return {
name: 'states:joke',
creator_opts: {
method: 'get',
echo: resp.data
}
};
});
} else if (choice.value == 'end') {
return 'states:end';
}
}
});
});
self.states.add('states:joke', function(name, opts) {
return new EndState(name, {
text: 'joke: ' + opts.echo.value.joke,
next: 'states:start'
});
});
self.states.add('states:end', function(name) {
return new EndState(name, {
text: 'Thanks, cheers!',
next: 'states:start'
});
});
});
return {
GoApp: GoApp
};
}();
go.init = function() {
var vumigo = require('vumigo_v02');
var InteractionMachine = vumigo.InteractionMachine;
var GoApp = go.app.GoApp;
return {
im: new InteractionMachine(api, new GoApp())
};
}();