mirror of
https://github.com/elisspace/FxLifeSheet.git
synced 2026-08-29 15:44:00 +00:00
Add support for triggering commands
This commit is contained in:
4
app.js
4
app.js
@@ -1,5 +1,6 @@
|
||||
// Third party dependencies
|
||||
var Moment = require("moment");
|
||||
var moment = require("moment");
|
||||
console.log(moment().week());
|
||||
// Telegram setup
|
||||
var Telegraf = require("telegraf");
|
||||
var Router = Telegraf.Router, Markup = Telegraf.Markup, Extra = Telegraf.Extra;
|
||||
@@ -60,6 +61,7 @@ function initBot() {
|
||||
" for question " +
|
||||
currentlyAskedQuestionKey);
|
||||
var dateToAdd = new Date();
|
||||
var weekOfYear = moment().week();
|
||||
var row = {
|
||||
Timestamp: dateToAdd.toLocaleString(),
|
||||
Year: dateToAdd.getFullYear(),
|
||||
|
||||
73
app.ts
73
app.ts
@@ -1,14 +1,11 @@
|
||||
// Third party dependencies
|
||||
const Moment = require("moment");
|
||||
const moment = require("moment");
|
||||
|
||||
// Telegram setup
|
||||
const Telegraf = require("telegraf");
|
||||
const { Router, Markup, Extra } = Telegraf;
|
||||
|
||||
const bot = new Telegraf(process.env.TELEGRAM_BOT_TOKEN);
|
||||
bot.start(ctx => ctx.reply("Welcome!"));
|
||||
bot.help(ctx => ctx.reply("Send me a sticker"));
|
||||
bot.on("sticker", ctx => ctx.reply("👍"));
|
||||
bot.hears("hi", ctx => ctx.reply("Hey there"));
|
||||
|
||||
// Sheets setup
|
||||
var GoogleSpreadsheet = require("google-spreadsheet");
|
||||
@@ -19,6 +16,10 @@ console.log("Loading " + process.env.GOOGLE_SHEETS_SHEET_ID);
|
||||
var doc = new GoogleSpreadsheet(process.env.GOOGLE_SHEETS_SHEET_ID);
|
||||
var sheet;
|
||||
|
||||
// State
|
||||
var currentlyAskedQuestionKey = null;
|
||||
var currentlyAskedQuestionMessageId = null;
|
||||
|
||||
async.series(
|
||||
[
|
||||
function setAuth(step) {
|
||||
@@ -60,10 +61,25 @@ async.series(
|
||||
|
||||
// App logic
|
||||
function initBot() {
|
||||
bot.hears(/(\d+)/, ({ match, reply }) => {
|
||||
let userValue = match[1];
|
||||
console.log("Got a new value: " + userValue);
|
||||
let dateToAdd = new Date(); // TODO: replace this with the date of the message
|
||||
bot.hears(/(\d+)/, ctx => {
|
||||
if (currentlyAskedQuestionMessageId == null) {
|
||||
ctx.reply(
|
||||
"Sorry, I forgot the question I asked, this usually means it took too long for you to respond, please trigger the question again by running the `/` command"
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
// user replied with a value
|
||||
let userValue = ctx.match[1];
|
||||
console.log(
|
||||
"Got a new value: " +
|
||||
userValue +
|
||||
" for question " +
|
||||
currentlyAskedQuestionKey
|
||||
);
|
||||
let dateToAdd = new Date();
|
||||
let weekOfYear = moment().week();
|
||||
|
||||
let row = {
|
||||
Timestamp: dateToAdd.toLocaleString(),
|
||||
Year: dateToAdd.getFullYear(),
|
||||
@@ -71,16 +87,49 @@ function initBot() {
|
||||
Day: dateToAdd.getDay(),
|
||||
Hour: dateToAdd.getHours(),
|
||||
Minute: dateToAdd.getMinutes(),
|
||||
Type: "Enough time for myself",
|
||||
Week: weekOfYear,
|
||||
Type: currentlyAskedQuestionKey,
|
||||
Value: userValue
|
||||
};
|
||||
console.log(row);
|
||||
|
||||
sheet.addRow(row, function(error, row) {
|
||||
reply("It's saved in the books for you");
|
||||
// TODO: replace with editing the existing message (ID in currentlyAskedQuestionMessageId, however couldn't get it to work)
|
||||
ctx.reply("Success ✅", Extra.inReplyTo(currentlyAskedQuestionMessageId));
|
||||
});
|
||||
});
|
||||
|
||||
// As we get no benefit of using `bot.command` to add commands, we might as well use
|
||||
// regexes, which then allows us to let the user's JSON define the available commands
|
||||
|
||||
bot.hears(/\/(\w+)/, ctx => {
|
||||
console.log(ctx);
|
||||
|
||||
// user entered a command to start the survey
|
||||
let command = ctx.match[1];
|
||||
if (command == "awake") {
|
||||
// Looks like Telegram has some limitations:
|
||||
// - No way to use `force_reply` together with a custom keyboard (https://github.com/KrauseFx/FxLifeSheet/issues/5)
|
||||
// - No way to update existing messages together with a custom keyboard https://core.telegram.org/bots/api#updating-messages
|
||||
ctx
|
||||
.reply(
|
||||
"How well did you sleep today?",
|
||||
Markup.keyboard([["7", "8", "9"], ["4", "5", "6"], ["1", "2", "3"]])
|
||||
.oneTime()
|
||||
.extra()
|
||||
)
|
||||
.then(({ message_id }) => {
|
||||
currentlyAskedQuestionMessageId = message_id;
|
||||
});
|
||||
currentlyAskedQuestionKey = "sleepQuality";
|
||||
}
|
||||
});
|
||||
|
||||
bot.start(ctx => ctx.reply("Welcome to FxLifeSheet"));
|
||||
|
||||
bot.help(ctx => ctx.reply("TODO: This will include the help section"));
|
||||
bot.on("sticker", ctx => ctx.reply("Sorry, I don't support stickers"));
|
||||
bot.hears("hi", ctx => ctx.reply("Hey there"));
|
||||
|
||||
// has to be last
|
||||
bot.launch();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user