Custom Form Facebook Messenger

Hello Rasa Community! I want to make a bot which make an order in my case (through Facebook Messenger)… For example i want to ask first the name of the customer. After the customer write me george. But there are 4 georges [‘george mariou’,‘george alexiou’,‘george xaris’,‘george tsiko’]. The db send me these 4 options. And I want to make a custom action which send these choices with json message in Facebook and after the customer choose the one that he/she wants and the slot take this value that choose. And after continue with other slots. How can I do this?

Hey @felicity,

This is a quick scribble of your requirements. I hope you can build from this

from typing import Any, Text, Dict, List
from rasa_sdk import Action, Tracker
from rasa_sdk.executor import CollectingDispatcher
from rasa_sdk.forms import FormAction
from rasa_sdk.events import SlotSet

class FindrightName(Action):
    def name(self) -> Text:
        """Unique identifier of the action"""
        return "FindrightName"

    def checkCustomerName(self, name):
        # Query your DB here, Right now I am doing it in list
        names = ["george_geroge_1", "george_2", "george_3",
                "Filp_1", "Flip_2", "Filip_3"]
        matches = []
        for e in names:
            if name in (e):
                matches.append(e)
        return matches
    def run(self,
            dispatcher: CollectingDispatcher,
            tracker: Tracker,
           domain: Dict[Text, Any]) -> List:
        #Get name input from the user
        name = (tracker.latest_message)['text']
        matched_names = self.checkCustomerName(name)
        print("Name", matched_names)
        buttons = []
        for t in matched_names:
            name = t
            # Add your next intent once the name got matched
            payload = "/matching_names{\"names\":\""+t+"\"}"
            buttons.append({
                "title": "{}".format(name),
                "payload": payload
            })
        dispatcher.utter_message(text="Test", buttons=buttons)
        return []

class FacilityForm(FormAction):
    def name(self) -> Text:
        """Unique identifier of the form"""
        return "facility_form"

    @staticmethod
    def required_slots(tracker: Tracker) -> List[Text]:
        """A list of required slots that the form has to fill"""
        return ["name"]

    def slot_mappings(self) -> Dict[Text, Any]:
        return {"name": self.from_entity(entity="names", intent="matching_names")}

    def submit(self,
               dispatcher: CollectingDispatcher,
               tracker: Tracker,
               domain: Dict[Text, Any]
               ) -> List[Dict]:

        name = tracker.get_slot('name')
        return [SlotSet("name", name)]

stories.md

Output from FB messenger below, However facebook supports three buttons maximum, when querying DB you must limit to 3 names. I ended the form with utter_welcome. You can continue from there.

Facebook Integration you can find here Rasa->FB

Hope it helps!

1 Like

Thank you very much for the instant response… This can work with quick_replies too?

Yes, It will work. I never tried it. I was procrastinating to go through FB documentation :sweat_smile:

1 Like