Setting and Import Code

Next, we write 2 command handlers. These are callback functions that will be called when the update is received. Let’s write two such functions for the /start command and for any plain text message. Two parameters are passed there as arguments: bot and update. Bot contains the necessary methods for interacting with the API, and update contains information about the incoming message.

Now we can test the performance of our new bot. We paste our API token on line 2, save the changes, move it to the console and run the bot:

python bot.py

After the launch, we write to him. If everything is configured correctly, you will see this:

The basis of the bot is written, we proceed to the next step!P.s. do not forget to turn off the bot, for this go back to the console and press Ctrl + C, wait a couple of seconds and the bot will successfully complete the work.

Step 3. Setting the AI

First of all, we go and register on Dialog flow (just log in with your Google account). Immediately after the authorization, we get to the control panel.

Now, select a Create agent button and fill in the fields at our discretion (this will not play any role, it is only necessary for the next action).

Click on Create and see the following picture:

I’ll tell you why the “Agent” we created earlier does not play any role. In the Intents tab, there are “commands” on which the bot works. Now he can only respond to phrases such as “Hello”, and if he does not understand, he answers “I did not understand you”. Not very impressive.After creating our empty agent, we have a bunch of other tabs. We need to click on Prebuilt Agents (these are already specially trained agents that have many teams) and select Small Talk from the entire list.

We click on it and click Import. Further nothing changing, click Ok. The agent was imported and now we can configure it. To do this, in the upper left corner click on the gear near the Small-Talk and get to the settings page. Now we can change the agent’s name as desired (I leave it as it was). We change the time zone and in the Languages tab, we check that the Russian language has been set (if not installed, then set).

Return to the General tab, go down a bit and copy Client access token

Now our AI is fully integrated, you can go back to the bot.

Step 4. Putting it all together

AI is ready, the base of the bot is ready, what’s next? Next, we need to download the wrapper API from Dialog flow for python.

pip install apiai

Installed? We return to our bot. Add to our “Settings” section the import of apiai and json modules (you need to disassemble the responses from dialogflow in the future). Now it looks like this:

The response is from the server, encoded in bytes. To decode it, we simply apply the method.

decode('utf-8')

And after that, we “wrap” everything in:

json.loads()

To disperse the json response.

If there is no answer (more precisely, json always comes, but there is not always an array with an AI response), it means that Small-Talk did not understand the user (you can study later). Therefore, if there is no “answer”, then we write to the user “I do not quite understand!”.Total, the full bot code with AI will look like this:

Step 5. The final part

I think you were convinced that writing a bot with AI is 10 minutes. It remains only now to teach and teach. You can do this, by the way, in the Training tab. There you can see all the messages that were written and that the bot responded to them (or did not answer). There it can also be trained, telling the bot where he answered correctly, and where not.

Paul Brook is a professional full stack developer & IT content marketer. Developer by day and a content Marketer by night. He is the lead Developer and content marketer at www.smartspate.com. His passion for helping people in all aspects IT and Marketing flows through in the expert industry coverage he provides. In addition to IT, also provide technical SEO and copywriting services for business of all sizes.

Find Us At:

error: This Content is Protected! All Rights belong to Smart Spate Ltd.

This website uses cookies. By continuing to use this website you are giving consent to cookies being used. For information on a cookie and how it impacts on users, you can visit our Privacy Policy and Cookie Policy.AcceptCloseRead more