-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathappgit.js
68 lines (58 loc) · 2.57 KB
/
appgit.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
var builder = require('botbuilder');
var restify = require('restify');
var githubClient = require('./github-client.js');
var connector = new builder.ChatConnector();
var bot = new builder.UniversalBot(connector);
var dialog = new builder.IntentDialog();
dialog.matches(/^search/i, [
function (session, args, next) {
if (session.message.text.toLowerCase() == 'search') {
builder.Prompts.text(session, 'Who are you looking for?');
} else {
var query = session.message.text.substring(7);
next({ response: query });
}
},
function (session, result, next) {
var query = result.response;
if (!query) {
session.endDialog('Request cancelled');
} else {
githubClient.executeSearch(query, function (profiles) {
var totalCount = profiles.total_count;
if (totalCount == 0) {
session.endDialog('Sorry, no results found.');
} else if (totalCount > 10) {
session.endDialog('More than 10 results were found. Please provide a more restrictive query.');
} else {
session.dialogData.property = null;
var usernames = profiles.items.map(function (item) { return item.login });
builder.Prompts.choice(session, 'What user do you want to load?', usernames,
{listStyle:builder.ListStyle.button});
}
});
}
}, function (session, result, next) {
var username = result.response.entity;
githubClient.loadProfile(username, function (profile) {
var card = new builder.ThumbnailCard(session);
card.title(profile.login);
card.images([builder.CardImage.create(session, profile.avatar_url)]);
if (profile.name) card.subtitle(profile.name);
var text = '';
if (profile.company) text += profile.company + ' \n';
if (profile.email) text += profile.email + ' \n';
if (profile.bio) text += profile.bio;
card.text(text);
card.tap(new builder.CardAction.openUrl(session, profile.html_url));
var message = new builder.Message(session).attachments([card]);
session.send(message);
});
}
]);
bot.dialog('/', dialog);
var server = restify.createServer();
server.listen(process.env.port || process.env.PORT || 3978, function () {
console.log('%s listening to %s', server.name, server.url);
});
server.post('/api/messages', connector.listen());