Help your Solidus customers with a chatbot

I want to show you how to make a Ruby bot for Telegram that can help your customers use your Solidus store through Wit.
In order to start you will have to:
- have a Wit account;
- have a Solidus store installed and configured with the API module enabled;
- have a Telegram account
Chatbots are the trend of the moment.
Since 2010 Apple with Siri has opened the way to a new concept: the personal assistant. Today a lot of companies want to use them to help customers without the need for human intervention. I’ll guide you how to create and configure your first bot to help your customers retrieving product information from Solidus
Create a Telegram chatbot
I chose Telegram to make the bot because it offers a very simple API and it’s fully compatible with Ruby. Following this guide you can make your first bot in less than 5 minutes:
- go to the BotFather chat;
- type
/start
; - run the
/newbot
command; - follow the instructions;
- take the token, we’ll use it later
Create your app on Wit
Sign in your Wit account and create your app here.
Insert common phrases which you expect your customers will use to interact with your bot.
Now teach it how to extract intent
and entities
from received messages.
After go to settings and take a note of Server Access Token
in API Details
panel.
Create a Solidus API key
The creation of an API key is required to use the Solidus API. You can generate it on the edit user page in the admin section. After that you can get the API key in the Rails console inspecting the user.
Code your chatbot
I wrote a very basic chatbot which uses Telegram API to capture user’s messages, routes them to Wit, returns them back after Wit has processed them and sends an answer to the user.
Let’s start by making a directory for your bot:
mkdir solidus_bot
cd solidus_bot
Create a Gemfile and put the following lines inside it:
source 'https://rubygems.org'
gem 'spree-api-client', github: 'burningpony/spree-api-client'
gem 'telegram-bot-ruby'
gem 'wit'
And run bundle install
.
The Wit side
Put the following code in a file named wit_client.rb
:
require 'wit'
class WitClient
def initialize(access_token, solidus_api_client)
@solidus_api_client = solidus_api_client
@wit_access_token = access_token
end
def solidus_client
@solidus_api_client
end
def wit
@wit = Wit.new(access_token: @wit_access_token)
end
def request(text)
response = wit.message(text)
intent = response.dig('entities', 'intent')&.first
raise Misunderstanding, "Can't understand #{response['_text']}" if intent['confidence'] < 0.9
case intent['value']
when 'search_product'
query_search = response.dig('entities', 'local_search_query')
raise Misunderstanding, "search param missing" if query_search.nil?
products = solidus_client.products(q: { name_cont: query_search.first['value'] })
products.map do |product|
[
product.name,
product.display_price,
product.master.images.first.large_url
].join "\n"
end.join("\n\n")
else
raise Misunderstanding, "Can't find intent #{intent['value']}"
end
rescue Misunderstanding => e
e.message
rescue StandardError => e
'Sorry, Something went wrong'
end
end
This detect the search_product
intent and execute a search on your store.
You can write a lot of intent to teach your bot how to resolve a lot of users’
problems.
The SolidusBot
Now create the entry point to our application solidus_bot.rb
and write:
require 'telegram/bot'
require 'spree-api-client'
require_relative 'wit_client'
telegram_token = 'TELEGRAM_TOKEN'
wit_access_token = 'WIT_ACCESS_TOKEN'
store_url = 'http://solidus.com/api'
store_api_url = "#{store_url}/api"
api_key = 'SOLIDUS_API_KEY'
class Misunderstanding < StandardError; end
WitClient.new(wit_access_token,
Spree::API::Client.new(store_api_url, api_key)).tap do |wit|
Telegram::Bot::Client.run(telegram_token) do |bot|
bot.listen do |message|
case message.text
when '/start'
bot.api.send_message(chat_id: message.chat.id, text: "Hi, #{message.from.first_name}")
when '/stop'
bot.api.send_message(chat_id: message.chat.id, text: "Bye, #{message.from.first_name}")
else
response = wit.request(message.text)
bot.api.send_message(chat_id: message.chat.id, text: response)
end
end
end
end
The code above generates clients which interact and will be used to call the Solidus store, execute a flow from Wit and exchange the messages with the user.
Remember to replace the Token and the API key with yours.
Try the bot
Run the bot with:
bundle exec ruby solidus_bot.rb
Now open Telegram and check that the bot returns the correct answer.
I really hope you enjoyed this tutorial. What do you think about it? I’d like to know your impressions, please leave a comment below and, if you have any question or need any help, feel free to ask.