Skip to content

Instantly share code, notes, and snippets.

@khanhkhuu
Last active December 4, 2023 02:49
Show Gist options
  • Save khanhkhuu/ae74b2c1b032f3c57d8a7255c8110c00 to your computer and use it in GitHub Desktop.
Save khanhkhuu/ae74b2c1b032f3c57d8a7255c8110c00 to your computer and use it in GitHub Desktop.
WordBot.js
function wordOrder() {
const sentences = [
"Sentence 1",
"Sentence 2",
"Sentence 3",
"Sentence 4",
];
const props = PropertiesService.getScriptProperties();
const nextCounter = props.getProperty('WORD_ORDER_COUNTER');
let counter = 1;
if (nextCounter) {
counter = Number(nextCounter);
}
let _nextCounter = counter + 1;
if (!sentences[_nextCounter]) _nextCounter = 1;
props.setProperty('WORD_ORDER_COUNTER', _nextCounter);
const selectedSentence = sentences[counter];
const translatedSentence = LanguageApp.translate(selectedSentence, 'en', 'vi');
const obfSentence = shuffleWords(selectedSentence);
const prevSentence = props.getProperty('PREV_SENTENCE');
props.setProperty('PREV_SENTENCE', JSON.stringify({
question: obfSentence,
answer: selectedSentence,
}));
const obfs = obfSentence.split(' ');
const firstWord = obfs.shift();
const lastWord = obfs.pop();
const payload = {
cardsV2: [
{
card: {
header: {
title: `<b>Sắp xếp lại từ</b>`,
},
sections: [
{
widgets: [
{
textParagraph: {
text: `<font color=\"#0870a1\">${firstWord}</font> <font color=\"#700f10\">${obfs.join(' ')}</font> <font color=\"#0870a1\">${lastWord}</font>\n\n${translatedSentence}`
}
}
]
}
]
}
}
]
}
if (prevSentence) {
const prev = JSON.parse(prevSentence);
payload.cardsV2[0].card.sections[0].widgets.unshift({
divider: {}
});
payload.cardsV2[0].card.sections[0].widgets.unshift({
textParagraph: {
text: `<b>Đáp án câu trước</b>:\n\t- ${prev.answer}`
}
});
}
UrlFetchApp.fetch(
'https://chat.googleapis.com/v1/spaces/Link_google_chat_webhook
, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
payload: JSON.stringify(payload),
});
}
function shuffleWords(sentence) {
let array = sentence.split(' ');
const firstWord = array.shift();
const lastWord = array.pop();
for (var i = array.length - 1; i > 0; i--) {
var j = Math.floor(Math.random() * (i + 1));
var temp = array[i];
array[i] = array[j];
array[j] = temp;
}
array.unshift(firstWord);
array.push(lastWord);
return array.join(' ');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment