Created
November 25, 2019 11:16
-
-
Save dmitryrogozhny/aa5b8416e37e8668fd6b8630265c7ef6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const Telegraf = require('telegraf'); | |
const Extra = require('telegraf/extra'); | |
const fs = require('fs'); | |
const path = require('path'); | |
console.log('Starting the bot...'); | |
const cities = [ | |
{ | |
name: 'Amsterdam', | |
id: 'Amsterdam', | |
}, | |
{ | |
name: 'Berlin', | |
id: 'Berlin', | |
}, | |
{ | |
name: 'Lisbon', | |
id: 'Lisbon', | |
}, | |
{ | |
name: 'Minsk', | |
id: 'Minsk', | |
}, | |
]; | |
function getData(city, functionDirectory) { | |
return new Promise((resolve, reject) => { | |
if (city.data) { | |
console.log('use from cache'); | |
return resolve(city.data); | |
} | |
console.log('read from file'); | |
const filePath = path.join(functionDirectory, `${city.id}.md`); | |
fs.readFile(filePath, (error, data) => { | |
if (error) { | |
console.log(error); | |
city.data = undefined; | |
return resolve('no data :('); | |
} | |
city.data = data.toString(); | |
return resolve(city.data); | |
}); | |
}); | |
} | |
const bot = new Telegraf(process.env["TELEGRAM_BOT_TOKEN"], { | |
webhookReply: true, | |
}); | |
bot.telegram.setWebhook(process.env["WEBHOOK_ADDRESS"]); | |
const welcomeMessage = (context) => { | |
return context.reply(`Hey ${context.from.first_name}!\nSelect a city where you'd like to have a great flat white:`, Extra.markup((m) => | |
m.inlineKeyboard( | |
cities.map((city) => m.callbackButton(city.name, city.id)) | |
))); | |
} | |
bot.on('callback_query', (context) => { | |
const cityId = context.update.callback_query.data; | |
const city = cities.filter((city) => city.id === cityId)[0]; | |
return context.answerCbQuery().then(() => { | |
getData(city, context.functionDirectory).then((data) => { | |
return context.replyWithMarkdown(data, { | |
disable_web_page_preview: true, | |
}); | |
}); | |
}); | |
}); | |
bot.on('sticker', welcomeMessage); | |
bot.hears(/^/, welcomeMessage); | |
bot.catch((err, ctx) => { console.log(`Error for ${ctx.updateType}`, err); }); | |
module.exports = async function (context, req) { | |
bot.context.functionDirectory = context.executionContext.functionDirectory; | |
console.log(context.executionContext.functionDirectory); | |
if (req.method === 'GET') { | |
return context.res = { | |
body: "" | |
}; | |
} | |
try { | |
const update = JSON.parse(req.rawBody); | |
bot.handleUpdate(update).catch((error) => { | |
console.log('Error processing update'); | |
console.log(error); | |
}); | |
} catch (error) { | |
console.error('Error parsing body', error); | |
return context.res = { | |
body: "" | |
}; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment