-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
84 lines (68 loc) · 2.54 KB
/
index.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
const builder = require('botbuilder')
const restify = require('restify')
const githubClient = require('./github-client.js')
const connector = new builder.ChatConnector()
const bot = new builder.UniversalBot(connector)
const dialog = new builder.IntentDialog()
dialog.matches(/^search/i, [
(session, args, next) => {
if (session.message.text.toLowerCase() == 'search') {
builder.Prompts.text(session, 'Who are you looking for?')
} else {
const query = session.message.text.substring(7)
next({
response: query
})
}
},
(session, result, next) => {
const query = result.response
if (!query) {
session.endDialog('Request cancelled')
} else {
githubClient.executeSearch(query, profiles => {
const 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
const cards = profiles.items.map(item => createCard(session, item))
const message = new builder.Message(session).attachments(cards).attachmentLayout('carousel')
session.send(message)
}
})
}
},
(session, result, next) => {
const username = result.response.entity
githubClient.loadProfile(username, profile => {
const 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)
let 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))
const message = new builder.Message(session).attachments([card])
session.send(message)
})
}
])
const createCard = (session, profile) => {
const card = new builder.ThumbnailCard(session)
card.title(profile.login)
card.images([builder.CardImage.create(session, profile.avatar_url)])
card.tap(new builder.CardAction.openUrl(session, profile.html_url))
return card
}
bot.dialog('/', dialog)
const server = restify.createServer()
server.listen(process.env.port || process.env.PORT || 3978, () => {
console.log('%s listening to %s', server.name, server.url)
})
server.post('/api/messages', connector.listen())