telegram-bot-calendar v1.0.7
Telegram Bot Calendar
telegram-bot-calendar
Overview
This package is for creating calendar UI in telegram bot messages. Easy to use comes with built-in tools to create calendar UI with ease.
Installation
npm install telegram-bot-calendar
Initialization
For JS
const calendar = require('telegram-bot-calendar');
For TS
import calendar from 'telegram-bot-calendar';
Getting first date of the month
This function will provide the Date
object for the first date of the provided date's month.
const todaysDate = Date.now();
const firstDate = calendar.getFirstDate(todaysDate);
Creating UI
This function will provide the JSON object, which needs to be passed with the message body while calling the send message API.
const todaysDate = Date.now();
const calendarUI = calendar.getUI(todaysDate);
Calling the send message API of Telegram bot with calendar UI.
const todaysDate = Date.now();
const calendarUI = calendar.getUI(todaysDate);
const sendMsgURL = `https://api.telegram.org/bot${process.env.telegramToken}/sendMessage`;
await axios.post(sendMsgURL,
{
chat_id: chat_id,
text: "Telegram Bot Calendar",
reply_markup: calendarUI
}
)
Handling callbacks
The callback needs to be handled with command type. The type
object specifies the type of command provided by the user using the calendar.
It can be either a date or command for next month's or the previous month's calendar.
const callbackData = data.callback_query.data;
const commandData = calendar.getCommandData(data.callback_query.data);
commandData
is an object consisting of two properties:
type
: it specifies the type of command passed by the user. It can be one of the seven listed below command types.providedDate
: This will be returned when the user clicks on the button displaying a valid date. In this case, thedata
object will consist of the date in the formatmonth/date/year
.nextMonth
: This will be returned when the clicks on theNext Month
button. It is an indication to display the next month.data
object, in this case, will consist of the first date of the displaying month.prevMonth
: This will be returned when the clicks on thePrevious Month
button. It is an indication to display to the previous month.data
object in this case will consist of the first date of the displaying month.currMonth
: This will be returned when the user clicks on the button displaying the calendar's current month or demanded month.day
: This will be returned when the user clicks on the button displaying one of the seven days of the week.dash
: This will be returned when the user clicks on the button displaying one of the empty buttons which are displaying as dash or hyphen '-'.notCalendarCommand
: This will be returned when the callback data provided to the getCommandData function is not a calendar command or not atelegram-bot-calendar
's generated callback data.
data
: it provides the information it can be eithernull
or have some value depending upon the type. This will consist of a valid date in case of :nextMonth
prevMonth
providedDate
This will be
null
in case of :day
currMonth
dash
notCalendarCommand
The date will provided in the format
month/date/year
. Eg. 2/19/2022
Creating next month's calendar
To create then next month's calendar, first date of the next month is required which can be easily generated using the getNextMonthFirstDate
.
const nextMonth = calendar.getNextMonthFirstDate(currDate);
const calendarUI = calendar.getUI(nextMonth);
In this way you can create next month's calendar UI. The currDate
can be the date provided by callback with nextMonth
command type.
Creating previous months calendar
To create then previous month's calendar, first date of the previous month is required which can be easily generated using the getPrevMonthFirstDate
.
const prevMonth = calendar.getPrevMonthFirstDate(currDate);
const calendarUI = calendar.getUI(prevMonth);
In this way you can create previous month's calendar UI. The currDate
can be the date provided by callback with prevMonth
command type.
Additional
If you want to delete the previously shown calendar you can use the telegram's delete message API.
const chat_id = data.callback_query.from.id;
const message_id = data.callback_query.message.message_id;
const deleteMsgURL = `https://api.telegram.org/bot${process.env.telegramToken}/deleteMessage`;
await axios.post(deleteMsgURL,
{
chat_id: chat_id,
message_id: message_id
}
)