How to run custom action from RASA having data source in XML format

Hi Team,

I am trying to get news from google RSS platform which is in XML format. I am trying to get data from actions.py but unable to do so. Below is my code. When I am typing “Tell me some news” or “News”, no data is showing at that time.

================================================= import requests import json import requests import urllib import urllib.request from urllib.request import urlopen from rasa_sdk import Action

class ActionGetNewst(Action):

def name(self):
    return 'action_get_news'

def run(self, dispatcher, tracker, domain):
    news_url="https://news.google.com/news/rss"
    Client=urlopen(news_url)
    xml_page=Client.read()
    Client.close()
    soup_page=soup(xml_page,"xml")
    news_list=soup_page.findAll("item")
    for news in news_list[:15]:
        dispatcher.utter_message(news.title.text)
    return []

In domain.yml I have added action as below: actions:

  • action_joke
  • action_get_news

In nlu.md I have added intent as below:

intent:news

  • Tell me some news
  • today’s news
  • news for today
  • current situation
  • current affairs

In stories I have added intent and action as below:

news path

  • news
    • action_get_news

Please help me

Hi @mitsiek !

I run only your code to get news. and I get this error: NameError: name 'soup' is not defined

Can be soup the problem?

On the other hand, make sure you are running:

rasa run actions

and the shell or other

rasa shell

Tell me if this help you

Hi @flore!

I did add “from bs4 import BeautifulSoup as soup” package but then too no response.

Below is the full actions.py code here?

import os import requests import json import urllib import urllib.request import logging import re import string import feedparser from urllib.request import urlopen from rasa_sdk import Action, Tracker from typing import Any, Text, Dict, List from rasa_sdk.executor import CollectingDispatcher from bs4 import BeautifulSoup as soup

logging.basicConfig(level=logging.DEBUG)

class ActionGetNewst(Action):

def name(self):
    return 'action_get_news'

def run(self, dispatcher, tracker, domain):
    news_url="https://news.google.com/news/rss"
    Client=urlopen(news_url)
    xml_page=Client.read()
    Client.close()
    soup_page=soup(xml_page,"xml")
    news_list=soup_page.findAll("item")
    for news in news_list[:15]:
        dispatcher.utter_message(news.title.text)
    return []

class ActionJoke(Action): def name(self): return “action_joke”

def run(self, dispatcher, tracker, domain):
  request = requests.get('http://api.icndb.com/jokes/random').json()  # make an api call
  joke = request['value']['joke']  # extract a joke from returned json response
  dispatcher.utter_message(text=joke)  # send the message back to the user
  return []

Thanks

@flore,

I am running rasa using docker-compose so after every change in actions.py, I am restarting rasa action-server service using below command. docker-compose restart

Thanks

Hi @mitsiek!

If I had to look for the error I would start by seeing if action_joke works, to discard problems on the actions server. If only the action_get_news does not work well, I would start debugging the code inside this action.

I tryed run the code again with from bs4 import BeautifulSoup as soup added, but I get another error with the parser. But I do not know if this error appears only to me by my environment.

Mmmm I think maybe yo could controll if you have the image to run with docker-compose with rasa and this new libraries that you need for this action installed.

There are many things that can go wrong

It worked for me. I think you need to uncomment the action server in your endpoints.yml:

...
# Server which runs your custom actions.
# https://rasa.com/docs/rasa/core/actions/#custom-actions/

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

# Tracker store which is used to store the conversations.
# By default the conversations are stored in memory.
# https://rasa.com/docs/rasa/api/tracker-stores/
...

Here’s the output:

]$ rasa shell
2020-07-17 10:20:53 INFO     root  - Connecting to channel 'cmdline' which was specified by the '--connector' argument. Any other channels will be ignored. To connect to all given channels, omit the '--connector' argument.
2020-07-17 10:20:53 INFO     root  - Starting Rasa server on http://localhost:5005
Bot loaded. Type a message and press enter (use '/stop' to exit):
Your input ->  current affairs
White House warns stimulus package ‘must’ include Trump payroll tax cut proposal - The Washington Post
Mark Zuckerberg, In Interview With Anthony Fauci, Criticizes Trump Administration’s Coronavirus Response - Deadline
US, Canada, UK accuse Russia of hacking COVID-19 vaccine trials - USA TODAY
Georgia mayors speak out against governor's ban on face mask mandates - CNN
Trump faces rising disapproval and widespread distrust on coronavirus, Post-ABC poll finds - The Washington Post
Republican National Convention eyes outdoor amphitheater for Trump Jacksonville speech - Miami Herald
President Trump demotes campaign manager Brad Parscale amid slipping poll numbers - CBS News
'Reckless, callous, cruel': teachers' chief denounces Trump plan to reopen schools - The Guardian
Supreme Court Deals Major Blow To Ex-Felons' Right To Vote In Florida - NPR
Amash indicates he won’t seek reelection to Congress - POLITICO
Live Coronavirus Cases Updates - The New York Times
Brazil surpasses 2 million coronavirus cases, with more than 76,000 dead - CBS News
Global report: coronavirus infections in India pass 1m mark - The Guardian
'Only going to get worse': After Hagia Sophia ruling, many fear what's next from Erdoğan - NBC News
British Airways retires its entire fleet of Boeing 747 jets - CNBC
Your input ->

TIP: Place start and end code blocks with three ` characters. This makes the blocks so much easier to read.