DEV Community

Cover image for Is it possible to create a ChatBot by batch(cmd script)?
AndréL
AndréL

Posted on

Is it possible to create a ChatBot by batch(cmd script)?

I believe that everyone who has been in this area of programming for some time has already heard or used batch script, especially in videos such as: Notepad Virus. And compared to bash (linux terminal script) it is very primitive and very limiting but in a day with nothing to do I thought: Can I get gold from cement?

So taking advantage of a very popular topic which are chatbots which are basically conversation robots I thought I'd try to do this in batch, even though batch has a default syntax I still had to spend a good few hours researching loops, variables and structures of comparison.

But let's finally get started, first create the bot.bat script and then open it in notepad to follow the ritual! So let's get down to business and start with the classic:

@echo off
Enter fullscreen mode Exit fullscreen mode

It's simply used to prevent the cmd from showing every command executed in the cmd, so let's keep taking user input:

@echo off

set /p user=You: 
echo %user%
Enter fullscreen mode Exit fullscreen mode

We use the set command to create a variable and we pass the /p parameter to the variable to receive the value of a user input and then we pass the user variable name and the = with the input text, then we use echo to show something in the screen and we pass the value of the user variable and in batch we need to pass the variable name between %%. After that we will create preprogrammed lines for our bot:

@echo off

set /p user=You: 
if %user%==hello echo Bot: Hello
Enter fullscreen mode Exit fullscreen mode

Basically we use the if comparison structure and we compare if the input value is the hi string, after the comparison we pass what will happen if the expression is true and finally we have the first response from the bot.

But we have the problem that the bot only receives one input from the user and already exits, so we need to make a loop!

@echo off

:loop:
set /p user=You: 
if %user%==hello echo Bot: Hello
goto loop
Enter fullscreen mode Exit fullscreen mode

But we have an infinite loop so we can make a little control system for the bot to exit when we type exit:

@echo off

set quit=0
:loop:
set /p user=You: 

if %user%==hello echo Bot: Hello

if %user%==exit set quit=1
if %quit%==0 goto loop
Enter fullscreen mode Exit fullscreen mode

Briefly we create a variable quit that starts with 0, if the user types exit then the variable quit gets 1, then if the variable quit equals 0 then the program goes back to the loop.

And after all this work we finally finished our bot and it's worth mentioning that for some reason I couldn't solve the comparison we can only compare the user variable with 1 word, but after all that I can say that this was much more complicated than it would be in a high level language and beyond that we can see that the batch is very limiting but after a lot of work we can see that it was possible to extract gold from the cement! And we finally finished this experiment and I say goodbye to you.

Top comments (0)