mirror of
https://github.com/elisspace/FxLifeSheet.git
synced 2026-08-29 15:44:00 +00:00
Implement more bot features
This commit is contained in:
56
app.js
56
app.js
@@ -2,11 +2,8 @@
|
||||
var Moment = require("moment");
|
||||
// Telegram setup
|
||||
var Telegraf = require("telegraf");
|
||||
var Router = Telegraf.Router, Markup = Telegraf.Markup, Extra = Telegraf.Extra;
|
||||
var bot = new Telegraf(process.env.TELEGRAM_BOT_TOKEN);
|
||||
bot.start(function (ctx) { return ctx.reply("Welcome!"); });
|
||||
bot.help(function (ctx) { return ctx.reply("Send me a sticker"); });
|
||||
bot.on("sticker", function (ctx) { return ctx.reply("👍"); });
|
||||
bot.hears("hi", function (ctx) { return ctx.reply("Hey there"); });
|
||||
// Sheets setup
|
||||
var GoogleSpreadsheet = require("google-spreadsheet");
|
||||
var async = require("async");
|
||||
@@ -14,6 +11,9 @@ var async = require("async");
|
||||
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) {
|
||||
// var creds = {
|
||||
@@ -48,11 +48,18 @@ async.series([
|
||||
});
|
||||
// App logic
|
||||
function initBot() {
|
||||
bot.hears(/(\d+)/, function (_a) {
|
||||
var match = _a.match, reply = _a.reply;
|
||||
var userValue = match[1];
|
||||
console.log("Got a new value: " + userValue);
|
||||
var dateToAdd = new Date(); // TODO: replace this with the date of the message
|
||||
bot.hears(/(\d+)/, function (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
|
||||
var userValue = ctx.match[1];
|
||||
console.log("Got a new value: " +
|
||||
userValue +
|
||||
" for question " +
|
||||
currentlyAskedQuestionKey);
|
||||
var dateToAdd = new Date();
|
||||
var row = {
|
||||
Timestamp: dateToAdd.toLocaleString(),
|
||||
Year: dateToAdd.getFullYear(),
|
||||
@@ -60,14 +67,39 @@ function initBot() {
|
||||
Day: dateToAdd.getDay(),
|
||||
Hour: dateToAdd.getHours(),
|
||||
Minute: dateToAdd.getMinutes(),
|
||||
Type: "Enough time for myself",
|
||||
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+)/, function (ctx) {
|
||||
console.log(ctx);
|
||||
// user entered a command to start the survey
|
||||
var 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(function (_a) {
|
||||
var message_id = _a.message_id;
|
||||
currentlyAskedQuestionMessageId = message_id;
|
||||
});
|
||||
currentlyAskedQuestionKey = "sleepQuality";
|
||||
}
|
||||
});
|
||||
bot.start(function (ctx) { return ctx.reply("Welcome to FxLifeSheet"); });
|
||||
bot.help(function (ctx) { return ctx.reply("TODO: This will include the help section"); });
|
||||
bot.on("sticker", function (ctx) { return ctx.reply("Sorry, I don't support stickers"); });
|
||||
bot.hears("hi", function (ctx) { return ctx.reply("Hey there"); });
|
||||
// has to be last
|
||||
bot.launch();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user