Sunday, May 31
Shadow

Build a voice bot to ask for your subscribers count | Rasa | Web scraping

Voice bot Voice bot Voice bot 🗣️

This technology is becoming important and a part of a life day by day as this technology has won the hearts of so many people. So many people are choosing this technology in the field of automation as this technology has made our lives so easy and comfortable that now we have spare a lot of time for use as well as for our loved ones.

To make you aware about this amazing technology, I’m here with my new blog to elaborate you about the web scraping and to use it with voice bot made with Rasa chatbot.

In this blog I will show you how to build a voice bot using RASA that can used as an assistant for you whom you can ask anything and the bol will be able to answer you using web scraping.

The example problem that I will elaborate in this session is of asking the bot about my active subscribers of my youtube channel. You can use this logic to find the active subscribers of any YouTube channel. This will be a very good start for you to learn web scraping with the voice bot based on Rasa.

Firstly build your first chatbot using RASA. If you are new to Rasa and you haven’t read my previous blog on how to build you first Rasa chatbot then check here and build your first chatbot and get started.

Now when you have you basic rasa chatbot ready with you, now modify the nlu, stories, endpoints and domain file as per the chatbot requirement in this project as shown below,

nlu.md

## intent: number_of_subscriber
- what is the number of subscriber of my channel on youtube?
- May i know what is my current subscriber count?
- how many subscriber do I have right now?

## intent: thanks
- thank you so much
- thanks
- thank you
- tnx

## intent:greet
- hey
- hello
- hi
- good morning
- good evening
- hey there

## intent:goodbye
- bye
- goodbye
- see you around
- see you later

## intent:affirm
- yes
- indeed
- of course
- that sounds good
- correct

## intent:deny
- no
- never
- I don't think so
- don't like that
- no way
- not really

## intent:mood_great
- perfect
- very good
- great
- amazing
- wonderful
- I am feeling very good
- I am great
- I'm good

## intent:mood_unhappy
- sad
- very sad
- unhappy
- bad
- very bad
- awful
- terrible
- not very good
- extremely sad
- so sad

## intent:bot_challenge
- are you a bot?
- are you a human?
- am I talking to a bot?
- am I talking to a human?

domain.yml

intents:
- greet
- goodbye
- affirm
- deny
- bot_challenge
- thanks
- number_of_subscriber
templates:
utter_greet:
- text: Hey! How may I assist you?
utter_anything_else:
- text: Is there anything else that I can help you with?
utter_goodbye:
- text: Bye
utter_iamabot:
- text: I am a bot, powered by Rasa.
utter_subscriber:
- text: Your subscriber count is {count}.
actions:
- utter_greet
- utter_iamabot
- utter_goodbye
- utter_anything_else
- action_subscriber
- utter_subscriber
stories.md

## happy path
* greet
- utter_greet

## subscriber path
* number_of_subscriber
- action_subscriber
* thanks
- utter_anything_else
* deny
- utter_goodbye

## say goodbye
* goodbye
- utter_goodbye

## bot challenge
* bot_challenge
- utter_iamabot
endpoints.yml

action_endpoint:
url: "http://localhost:5055/webhook"

Now when you have made the change in all the mentioned files, train your model using “rasa train” and check it. When it’s working fine, then convert your Rasa chatbot to the voice bot as already done in the previous blog. If you are new here and haven’t gone through that blog, then check it here to convert your Rasa chatbot to a voice bot:

Once you have your voice bot ready with you and it’s working file then it’s time to create our program for web scraping to fetch the active subscribers of my YouTube channel.

Firstly, install the packages in your existing environment where you have all your packages installed for Rasa chatbot using the given command,

pip install requests bs4

This will install both the packages and now we can start writing out code for it. Here is the code for the web scraping for fetching the active subscribers count of my channel.

Code for subscribers.py :

import requests
from bs4 import BeautifulSoup

def subscriber(url):
    page = requests.get(url)
    print(page)

    soup = BeautifulSoup(page.text, "html.parser")
    print(soup)
    pew = soup.findAll('span',
                       {'class': 'yt-subscription-button-subscriber-count-branded-horizontal subscribed yt-uix-tooltip'})
    print(pew)

    for subs in pew:
        print("Innovate Yourself subscribers: ",subs.get_text())

    return subs.get_text()

So here you have the code to fetch the subscribers count. In this script there is one function named subscriber which takes the url or channel as an argument as an input and fetched the subscriber count. Firstly, the complete url page is read from the url and a get request is send to the url to fetch the web page and with respect to that a response code is returned along with the complete html web page. This returned code symbolizes that you have successfully got the output with code 200 or you have got any error, while parsing the page with code other than 200.

page = requests.get(url)

When you have got the response code as 200 then you can move to the next step to parse the data from the “page” with respect to the html parser with

soup = BeautifulSoup(page.text, "html.parser")

when this will parse all the data from the webpage w.r.t. the html parser using BeautifulSoup we can find the data from it with span and class as shown here,

pew = soup.findAll('span',
{'class': 'yt-subscription-button-subscriber-count-branded-horizontal subscribed yt-uix-tooltip'})

Now with this class value we will check for the text associated with this and that text will be the subscriber count of the channel.

for subs in pew:
print("Innovate Yourself subscribers: ",subs.get_text())

This will give the subscriber count, now return it and use it with your action file to make it work with your voice bot. Make changes in the actions file as shown here,

# This files contains your custom actions which can be used to run
# custom Python code.
#
# See this guide on how to implement these action:
# https://rasa.com/docs/rasa/core/actions/#custom-actions/


# This is a simple example for a custom action which utters "Hello World!"

from typing import Any, Text, Dict, List

from rasa_sdk import Action, Tracker
from rasa_sdk.executor import CollectingDispatcher
from subscribers import subscriber

class ActionHelloWorld(Action):

def name(self) -> Text:
return "action_subscriber"

def run(self, dispatcher: CollectingDispatcher,
tracker: Tracker,
domain: Dict[Text, Any]) -> List[Dict[Text, Any]]:
url = 'https://www.youtube.com/channel/UCzPjCodug7GuQk4cCbHipHQ'
count=subscriber(url)

dispatcher.utter_template('utter_subscriber', tracker, count=count)

return []

Once you have completed this, you are good to go. Run your voice bot and actions server and watch it in action with web scraping.

To get the full code check this link on github:
https://github.com/ashus3868/RasaSubscriberCount.git

Watch the full session of how to Build a voice bot to ask for your subscribers count with rasa and Web scraping here:

Stay safe and stay healthy, Happy Learning. 🙂

For any doubts or queries related to the topic, feel free to leave a comment below in the comment section. Also, do comment your valuable feedback to appreciate us. For any further information or queries, you can also WhatsApp me at +91 8209829808.

Stay tuned and happy learning.

Leave a Reply

Your email address will not be published. Required fields are marked *