adam-saudagar avatar

Ep.5 Console Command | Supbot Whatsapp Chatbot API

adam-saudagar

Published: 06 Nov 2018 › Updated: 06 Nov 2018Ep.5 Console Command | Supbot Whatsapp Chatbot API

Ep.5 Console Command | Supbot Whatsapp Chatbot API

small banner.jpg

Repository

https://github.com/adsau59/supbot

What Will I Learn?

  • What is console command?
  • How to use core console commands?
  • How to create console command?

Requirements

Required Knowledge

  • Java

Resources for this Tutorial:

Difficulty

✔Basic
Intermediate
Advanced

Description

Outline and Overview

In this Supbot tutorial we will take a look on Console Commands.

What is Console Command?

During development, there could come a need to create custom code snippets that you want to execute during the execution of the bot, but writing it anywhere would disturb the Supbot's threads pipeline .

In in order to do the same, console commands can be used,

  • Console Commands are commands that can be entered in the console during the execution of the bot.
  • It is used to execute custom functions with or without arguments.
How to use Console Command?

After you execute the bot, whatsapp web will get initialized. After the initialization is done, a greater than sign will appear in the console (>), here you can enter your console command. First the command keyword has to be written, followed by its arguments, like keyword arg1 arg2.

In order to learn how to use console command, we will see the help & run command.

Help Command
  • help command is used to show the available commands that you can use and it also shows the usage of the target commands

  • just type help in the console without any arguments and hit enter to show a list of all the console commands available in the bot.

    help.PNG

  • If you want to check the usage of any command type help followed by keyword of the command you want to check the usage of.

  • for example, help quit

    help_quit.PNG

  • The usage shows the name of the command, its description, its template and an example of how to use it.

Run Command
  • run command is used to run feature commands, which is used by the clients in whatsapp, using console commands.

  • Using run command, Supbot creates a temporary client which has the highest privileges that is Role.SuperAdmin, which can run any command in any group even if it's feature is not added in the group, it is the only way to promote a Co-Admin to admin and demote Admin to Co-Admin.

    help_run.PNG

  • The first argument of the run command is the group ID of the group you want the run the command in, as I explained before, it creates a temporary client which will belong to this group..

  • Second argument is the command keyword, which shows which feature command to run, remember you don't have to use the command prefix (;;) here.

  • Third and the next arguments are the arguments for the feature command which has to be executed, if the feature command doesn't take any arguments you can leave this empty.

    Screenshot (6).png

  • The result of this command isn't displayed in the console, but it shows the result directly inside the whatsapp chat group.

How to create Console Command?

You can create your own console command to run any code snippet you want, in order to do that you have to,

quitcc.PNG

  1. Create a class and extend it with ConsoleCommand.
  2. Implement its abstract methods and constructor.
  3. Add the console command in the bot using Bot.getConsole().getConsoleCommandManager().add() method in the addThingsInBot() method in the main class.

Lets have a look at the constructor and all the abstract methods that you have to implement to create Console Command

Constructor

cc_constructor.PNG

  • Constructor of the ConsoleCommand takes 2 parameters, keyword and noOfArgs.
  • keyword is the string which is used to run the command.
  • noOfArgs is the number of arguments required to use the command, you can keep this value -1 in order to skip number of argument check for variable number of arguments.
Compute Method

cc_compute.PNG

  • compute(String[] args) method is the method which is executed when the command is entered in the console.
  • It contains one parameter, called args, it is a string array which contains the arguments passed from the console.
getHelper Method

cc_gethelper.PNG

  • getHelper() method is used to create usage helper which can be shown using help command.
  • It returns a helper object which has three string attributes,
    • Template, should contain the usage of the command,
    • Example, should contain an example of using the command,
    • Description, should contain an short description of the command.
What we will make?
  • In this episode we will create a broadcast console command.
  • It can be sued to send message to all the groups simultaneously with one command, it can be used to send updates like "Bot will go under maintenance in 10 mins".

By creating this console command, you will learn where console command is used, and how to make it so that during the development of your own features, you wont have to waste time making a work around for it.

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.5 Console Command | 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