adam-saudagar avatar

Ep.4 Scheduler | Supbot Whatsapp Chatbot API

adam-saudagar

Published: 28 Oct 2018 › Updated: 28 Oct 2018Ep.4 Scheduler | Supbot Whatsapp Chatbot API

Ep.4 Scheduler | Supbot Whatsapp Chatbot API

small banner.jpg

Repository

https://github.com/adsau59/supbot

What Will I Learn?

  • What is Scheduler used for in Supbot API?
  • How Scheduler works in Supbot API?
  • How to use Scheduler in Supbot API?

Requirements

Required Knowledge

Resources for this Tutorial:

Difficulty

Basic
✔Intermediate
Advanced

Description

Outline and Overview

In this Supbot tutorial we will take a look on Scheduler.

What is Scheduler used for?

It is used in two ways,

  • Periodic Task,
    Tasks which are performed on an interval, like daily, weekly, yearly tasks
  • Timed Task,
    Tasks which are scheduled for a specific time, like reminders
How does Scheduler works?

Scheduler in Supbot API works using 3 componnets,

  • SchdeuleTask
  • Schedule
  • ScheduleManager
ScheduleTask

scheduletask.PNG

ScheduleTask is an interface with 3 methods,

  • task(): This method is used is executed when the schedule is arrived.
  • taskMissed(): This method is used is executed when the schedule is missed.
  • getNextSchedule(): This method is used to reschedule the date for the next schedule, can return null if the schedule is to be destroyed after running.
  • ScheduleTask implements Serializable.
Schedule

schedule_constructor.PNG

  • Schedule class encapsulates ScheduleTask, the date for schedule and the timer object.
  • Schedule also calculates the delay for the Schedule when the bot is launched.
ScheduleManager

schedule_manager_add.PNG

ScheduleManager has two jobs,

  • First is to add the schedule to the database when it is added,
  • and Second is to reload the schedule back when the bot is started.
How to use Scheduler?

There are two methods to use Scheduler,

  • Static Method
  • Dynamic Method
Static Method

schedule_cc.PNG

Static Method of creating Scheduler is used when you want to create the schedule yourself and has to be initialized just once.
In order to create Scheduler using static method you have to,

  • Create a class which implements ScheduleTask.
  • Add the class object of the class you made into ScheduleTaskInitializer using Bot.getScheduleTaskInitializer.add() method in the addThingsInBot method in the Main class.
  • then use schedule create console command to initialize the Schedule.
Dynamic Method

schedule_manager_add_use.PNG

Dynamic Method of creating Scheduler is used when you want the clients to create the schedule or it has to be initialized multiple times.
In order to create Scheduler using dynamic method you have to,

  • Create a class which implements ScheduleTask.
  • Create a Schedule Object using it.
  • Add it into ScheduleManager using Bot.getScheduleManager.add() method.

After we learn about Scheduler, we will look both of these methods in the video tutorial by creating,

  • GoodMorning Scheduler, which sends good morning messege every morning, and
  • RemindMe Command, which creates a reminder for the time the client specifies using natty API

Creating the GoodMorning Scheduler you will learn how to create Schedule using Static method and creating RemindMe Command you will learn how to create Schedule using Dynamic method.

This will create an understanding for you by which you can use both of these methods when it is required when you create your own features.

Video Tutorial

Curriculum

Contact

If you have any problems or you want to contact me on DefineX Community discord server

Proof of Work Done

https://github.com/adsau59

Leave Ep.4 Scheduler | Supbot Whatsapp Chatbot API to:

Written by

Game Developer, Gamer, Otaku :D

Read more #utopian-io posts


Best Posts From adam-saudagar

We have not curated any of adam-saudagar's posts yet. But you can encourage our curation team to review posts by visiting them regularly and by referring other readers. Because we give priority to frequently read content.

More Posts From adam-saudagar