diff --git a/functions/src/actions.ts b/functions/src/actions.ts index 2a3e87b..fc64d95 100644 --- a/functions/src/actions.ts +++ b/functions/src/actions.ts @@ -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', @@ -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 = { @@ -80,7 +86,7 @@ app.intent(INTENTS.FETCH_TRENDING, conv => { ])); } else { data.currentIndex = -1; - nextRepo(conv); + goToNextRepo(conv); } }) .catch(err => { @@ -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 => { @@ -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 @@ -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; @@ -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, diff --git a/functions/src/github.ts b/functions/src/github.ts index b091ce1..b39307c 100644 --- a/functions/src/github.ts +++ b/functions/src/github.ts @@ -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}`, + }, + }); +} diff --git a/functions/src/prompts.ts b/functions/src/prompts.ts index 2d881ce..75a2968 100644 --- a/functions/src/prompts.ts +++ b/functions/src/prompts.ts @@ -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!' +]; diff --git a/functions/src/utils.ts b/functions/src/utils.ts index fad29c9..61789c1 100644 --- a/functions/src/utils.ts +++ b/functions/src/utils.ts @@ -38,3 +38,12 @@ export function wrapWithSpeak(array) { '' ].join(''); } + +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); +}