Skip to content

Commit

Permalink
new feature: star repository
Browse files Browse the repository at this point in the history
  • Loading branch information
PinkyJie committed Apr 20, 2018
1 parent 13d76c6 commit 9526643
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 12 deletions.
49 changes: 42 additions & 7 deletions functions/src/actions.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
import {
dialogflow,
SimpleResponse,
RichResponse,
BasicCard,
Suggestions,
Button,
DialogflowConversation,
Contexts,
SignIn,
} from 'actions-on-google';

import { fetchTrending } from './github';
import { fetchTrending, starRepository } from './github';
import { UserData } from './types';
import * as PROMPTS from './prompts';
import { getRepoParagraph, getRandomMessage, getRepoStartMessage, wrapWithSpeak } from './utils';
import {
getRepoParagraph,
getRandomMessage,
getRepoStartMessage,
wrapWithSpeak,
getStarredMessage,
} from './utils';

const INTENTS = {
WELCOME: 'Default Welcome Intent',
Expand All @@ -22,6 +27,7 @@ const INTENTS = {
FETCH_TRENDING_NO_INPUT: 'Fetch Trending - No Input',
FETCH_TRENDING_NEXT_YES: 'Fetch Trending - Yes',
FETCH_TRENDING_NEXT_NO: 'Fetch Trending - No',
STAR_IT: 'Fetch Trending - Star It',
};

const PARAMETERS = {
Expand Down Expand Up @@ -80,7 +86,7 @@ app.intent(INTENTS.FETCH_TRENDING, conv => {
]));
} else {
data.currentIndex = -1;
nextRepo(conv);
goToNextRepo(conv);
}
})
.catch(err => {
Expand All @@ -90,7 +96,7 @@ app.intent(INTENTS.FETCH_TRENDING, conv => {
});

app.intent(INTENTS.FETCH_TRENDING_NEXT_YES, conv => {
nextRepo(conv);
goToNextRepo(conv);
});

app.intent(INTENTS.FETCH_TRENDING_NEXT_NO, conv => {
Expand All @@ -112,6 +118,34 @@ app.intent(INTENTS.FETCH_TRENDING_NO_INPUT, conv => {
handleReprompt(conv, PROMPTS.NO_INPUT_MORE_REPOSITORIES);
});

app.intent(INTENTS.STAR_IT, conv => {
console.log('TTTTT');
const data = conv.data as UserData;
const { currentIndex, repositories } = data;
const currentRepo = repositories[currentIndex];

const token = conv.request.user.accessToken;
if (token) {
return starRepository(currentRepo, token)
.then(() => {
conv.contexts.set(CONTEXTS.FETCH_TRENDING_FOLLOWUP, DEFAULT_CONTEXT_LIFE_SPAN);
conv.ask(wrapWithSpeak([
getStarredMessage(currentRepo),
getRandomMessage(PROMPTS.MORE_REPOSITORIES),
]));
})
.catch(err => {
console.log('Error: ', err);
conv.close(PROMPTS.NETWORK_ERROR);
});
} else {
return Promise.resolve()
.then(() => {
conv.ask(new SignIn());
});
}
});

export default app;

// Useful functions
Expand All @@ -125,7 +159,7 @@ function handleReprompt(conv: CONV_TYPE, messages) {
}
}

function nextRepo(conv: CONV_TYPE) {
function goToNextRepo(conv: CONV_TYPE) {
const data = conv.data as UserData;
const { repositories } = data;
data.currentIndex += 1;
Expand Down Expand Up @@ -158,6 +192,7 @@ function nextRepo(conv: CONV_TYPE) {
conv.ask(
new Suggestions(
getRandomMessage(PROMPTS.REPOSITORY_NEXT_BUTTON),
getRandomMessage(PROMPTS.REPOSITORY_STAR_BUTTON),
getRandomMessage(PROMPTS.REPOSITORY_GOODBYE_BUTTON),
),
cardItem,
Expand Down
9 changes: 9 additions & 0 deletions functions/src/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,12 @@ export function fetchTrending(
return repos;
});
}

export function starRepository(repo: Repository, token: string) {
const url = `https://api.github.com/user/starred/${repo.author}/${repo.name}`;
return axios.put(url, null, {
headers: {
Authorization: `token ${token}`,
},
});
}
16 changes: 11 additions & 5 deletions functions/src/prompts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,25 @@ export const REPOSITORY_LAST_ONE = [
'We have no more trending repositories today.',
'You\'ve mastered all trending repositories today!',
];
export const GOODBYE = [
'Okay! We can stop right there, Bye bye!',
'Alright! See you later! Bye!',
'OKay! Please come later to know more! Goodbye!'
];
export const REPOSITORY_NO_MORE = 'Sorry to hear that you are not interested!';
export const REPOSITORY_OTHER_LANGUAGE = 'If you want to know the trending for a specific languages, just ask me "Tell me the trending repositories for Javascript today".';

export const REPOSITORY_NEXT_BUTTON = [
'Explore more',
'Next',
'Go on',
];
export const REPOSITORY_STAR_BUTTON = [
'I Like it',
'Star it',
];
export const REPOSITORY_GOODBYE_BUTTON = [
'Not interested',
'No more',
];

export const GOODBYE = [
'Okay! We can stop right there, Bye bye!',
'Alright! See you later! Bye!',
'OKay! Please come later to know more! Goodbye!'
];
9 changes: 9 additions & 0 deletions functions/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,12 @@ export function wrapWithSpeak(array) {
'</speak>'
].join('<break />');
}

export function getStarredMessage(repo: Repository) {
const messages = [
`Cool! Repository ${repo.name} is already starred for you on Github.`,
`Done! Repository ${repo.name} is in your star list on Github.`,
`Alright! Repository ${repo.name} is starred, go to Github and check it out.`,
];
return getRandomMessage(messages);
}

0 comments on commit 9526643

Please sign in to comment.