How to Use Scala to Build a Chatbot

KaylaMatthews
Chatbots Magazine
Published in
3 min readAug 16, 2018

--

Scala is a programming language designed to provide support for a strong static type system and functional programming. Developed as a response to criticisms of Java, Scala is a compromise between the needs of industry and academic research.

The powerful tools Scala provides allows users to feel more in control of the programming and bots they’re creating. Scala enables developers to solve their problems more sustainably and offers typed and functional programming for users who are comfortable with either or both.

Scala makes building and using bots easier for those who already understand coding.

Using Someone Else’s Bot

The first step to using a bot already available is to find one that meets your needs. Bots can do anything from running security checks to sorting e-mail to helping you find a new job. Figure out what you need your bot to do and start your search to find one that satisfies all your needs.

Once you find the bot for you, create a new Scala application using sbt. After opening your build.sbt, also open the src/main/resources/application.conf file. This is where you add the API token you got from the bot to allow you to begin working.

From here, you can customize the bot within the parameters its developers allow. Depending on the bot and coding, you can change the actions, reactions and messages the bot is meant to provide. Just make sure you don’t make the bot unusable in your customization tasks.

Creating Your Own Bot

Making your bot from scratch is easier than it may seem once you understand what you need to do and have all the necessary tools available. Scala makes creating bots easier by allowing users multiple programming options.

Depending on what kind of bot you’re making, there are three things every chatbot needs to function:

1. The MessageEventBus has to be used to communicate with the bot

2. The help method defines commands for the user

3. The act or receive method makes the bot respond to commands

The help method usually requires a String => OutboundMessage function, while the OutboundMessage represents a message sent back to the bot. From here, the act method can be implemented with the Command method and the OutboundMessage.

The Commands are sent using a particularly typed name with a leading command character at the start, usually a symbol like $ or ! by default. When putting the bot together, put in the commands without the command character with the List[String] arguments and raw message.

Testing Your Bot

Testing out the bot yourself is as easy as opening the program and getting to work. Start the bot using the command sbt clean run. If you implemented everything correctly, your bot should go online. You can send direct messages to the bot, testing out all the commands and functions you just added in, including the help method.

If something’s wrong that you can’t identify, you can always get others to test the bot for you. Websites and communities dedicated to testing out your bot exist all over the internet. These testers will then provide feedback in the forms of criticisms and tips to help you make your bot better.

Scala and Programming

Automation is becoming more normal as time goes on, including in our everyday lives. Anyone can have the tools to make a bot for automating any task they need.

The future of AI lies in putting the opportunity in everyone’s hands and seeing what the world will have to offer. Scala is one more way to make the process easier for anyone to pick up.

--

--

tech and productivity writer. bylines: @venturebeat, @makeuseof, @motherboard, @theweek, @technobuffalo, @inc and others.