|
| 1 | +from typing import Dict, Text, Any, List, Union, Optional |
| 2 | + |
| 3 | +from rasa_sdk import Tracker |
| 4 | +from rasa_sdk.executor import CollectingDispatcher |
| 5 | + |
| 6 | +from rasa_sdk import Action |
| 7 | +from rasa_sdk.forms import FormAction |
| 8 | +import json |
| 9 | + |
| 10 | +import requests |
| 11 | +from dotenv import load_dotenv |
| 12 | +from pathlib import Path |
| 13 | +import os |
| 14 | +env_path = Path('.') / '.env' |
| 15 | +load_dotenv(dotenv_path=env_path) |
| 16 | +'''os.getenv() used to read varibles from .env file''' |
| 17 | +ticket_url = os.getenv("all_tikcket_url") |
| 18 | +apikey=os.getenv("api_key") |
| 19 | + |
| 20 | +def show_all(): |
| 21 | + response = requests.get(ticket_url,auth=(apikey, 'X')) |
| 22 | + return response.json() |
| 23 | +class AllTickets(Action): |
| 24 | + """example of custom action""" |
| 25 | + def name(self): |
| 26 | + """name of the custom action""" |
| 27 | + return "action_all_tickets" |
| 28 | + |
| 29 | + def run(self,dispatcher,tracker,domain): |
| 30 | + """action for display all tickets in freshdesk""" |
| 31 | + data=show_all() |
| 32 | + leng=len(data) |
| 33 | + for i in range(leng): |
| 34 | + message=str((i+1))+'.'+data[i]['subject']+'\n'+'Created Date:'+data[i]['created_at'] |
| 35 | + dispatcher.utter_message(message) |
| 36 | +def CreateTicket(description,subject,email,priority,status): |
| 37 | + """api call for create aticket in freshdesk help desk""" |
| 38 | + headers = { |
| 39 | + 'Content-Type': 'application/json', |
| 40 | + } |
| 41 | + |
| 42 | + data = { "description": description, "subject": subject, "email": email, "priority": priority, "status": status} |
| 43 | + json_data=json.dumps(data); |
| 44 | + |
| 45 | + response = requests.post(ticket_url, headers=headers, data=json_data, auth=(apikey, 'X')) |
| 46 | + |
| 47 | + return response |
| 48 | +class createTicketForm(FormAction): |
| 49 | + """Example of a custom form action""" |
| 50 | + def name(self): |
| 51 | + """Unique identifier of the form""" |
| 52 | + return "create_ticket_form" |
| 53 | + |
| 54 | + def required_slots(self,tracker) -> List[Text]: |
| 55 | + """A list of required slots that the form has to fill""" |
| 56 | + return ["description","subject","email","priority","status"] |
| 57 | + def slot_mappings(self) -> Dict[Text,Union[Dict, List[Dict]]]: |
| 58 | + """A dictionary to map required slots to |
| 59 | + - an extracted entity |
| 60 | + - intent: value pairs |
| 61 | + - a whole message |
| 62 | + or a list of them, where a first match will be picked""" |
| 63 | + |
| 64 | + |
| 65 | + return { |
| 66 | + "description": [ |
| 67 | + self.from_text(), |
| 68 | + ], |
| 69 | + "subject": [ |
| 70 | + self.from_text(), |
| 71 | + ], |
| 72 | + "email": [ |
| 73 | + self.from_text(), |
| 74 | + ], |
| 75 | + "priority": [ |
| 76 | + self.from_text(), |
| 77 | + ], |
| 78 | + "status": [ |
| 79 | + self.from_text(), |
| 80 | + ], |
| 81 | + |
| 82 | + |
| 83 | + } |
| 84 | + def submit( |
| 85 | + self, |
| 86 | + dispatcher: CollectingDispatcher, |
| 87 | + tracker: Tracker, |
| 88 | + domain: Dict[Text, Any], |
| 89 | + ) -> List[Dict]: |
| 90 | + """Define what the form has to do |
| 91 | + after all required slots are filled""" |
| 92 | + dispatcher.utter_message("Your ticket creation status in freshdesk") |
| 93 | + return [] |
| 94 | + |
| 95 | +class CreatenewTicket(Action): |
| 96 | + """example of custom action""" |
| 97 | + def name(self): |
| 98 | + """name of the custom action""" |
| 99 | + return "action_create_tickets" |
| 100 | + def run(self,dispatcher,tracker,domain): |
| 101 | + """action for create ticket in freshdesk""" |
| 102 | + descri=tracker.get_slot("description") |
| 103 | + sub=tracker.get_slot("subject") |
| 104 | + email=tracker.get_slot("email") |
| 105 | + prio=tracker.get_slot("priority") |
| 106 | + sta=tracker.get_slot("status") |
| 107 | + priority_type=int(prio) |
| 108 | + status_code=int(sta) |
| 109 | + data=CreateTicket(descri,sub,email,priority_type,status_code) |
| 110 | + if(data.status_code==201): |
| 111 | + dispatcher.utter_message('The ticket is sucessfully created in freshdesk') |
| 112 | + else: |
| 113 | + dispatcher.utter_message('Something bad happend while ticket creation') |
| 114 | + |
| 115 | +class UpdateTicket(Action): |
| 116 | + """example of custom action""" |
| 117 | + def name(self): |
| 118 | + """name of the custom action""" |
| 119 | + return "action_update_tickets" |
| 120 | + def run(self,dispatcher,tracker,domain): |
| 121 | + data=show_all() |
| 122 | + leng=len(data) |
| 123 | + dispatcher.utter_message("Here is your Ticket details") |
| 124 | + for i in range(leng): |
| 125 | + message="Ticket Id:"+str(data[i]['id'])+"\n"+"Ticket Subject:"+data[i]['subject'] |
| 126 | + dispatcher.utter_message(message) |
| 127 | + |
| 128 | +class updateTicketForm(FormAction): |
| 129 | + """Example of a custom form action""" |
| 130 | + def name(self): |
| 131 | + """Unique identifier of the form""" |
| 132 | + return "update_ticket_form" |
| 133 | + |
| 134 | + def required_slots(self,tracker) -> List[Text]: |
| 135 | + """A list of required slots that the form has to fill""" |
| 136 | + return ["priority-up","status-up","ticket_id"] |
| 137 | + def slot_mappings(self) -> Dict[Text,Union[Dict, List[Dict]]]: |
| 138 | + """A dictionary to map required slots to |
| 139 | + - an extracted entity |
| 140 | + - intent: value pairs |
| 141 | + - a whole message |
| 142 | + or a list of them, where a first match will be picked""" |
| 143 | + |
| 144 | + |
| 145 | + return { |
| 146 | + "priority-up": [ |
| 147 | + self.from_text(), |
| 148 | + ], |
| 149 | + "status-up": [ |
| 150 | + self.from_text(), |
| 151 | + ], |
| 152 | + "ticket_id": [ |
| 153 | + self.from_text(), |
| 154 | + ], |
| 155 | + |
| 156 | + |
| 157 | + } |
| 158 | + def submit( |
| 159 | + self, |
| 160 | + dispatcher: CollectingDispatcher, |
| 161 | + tracker: Tracker, |
| 162 | + domain: Dict[Text, Any], |
| 163 | + ) -> List[Dict]: |
| 164 | + """Define what the form has to do |
| 165 | + after all required slots are filled""" |
| 166 | + dispatcher.utter_message("Here is the update information") |
| 167 | + return [] |
| 168 | +def updateticket(ticket,priority,status): |
| 169 | + """api call for update a tiket in freshdesk""" |
| 170 | + headers = { |
| 171 | + 'Content-Type': 'application/json', |
| 172 | + } |
| 173 | + data = { "priority": priority, "status": status } |
| 174 | + json_data=json.dumps(data); |
| 175 | + url=ticket_url+'/'+str(ticket) |
| 176 | + |
| 177 | + response = requests.put(url, headers=headers, data=json_data, auth=(apikey, 'X')) |
| 178 | + return response |
| 179 | + |
| 180 | +class UpdatedTicket(Action): |
| 181 | + """example of custom action""" |
| 182 | + def name(self): |
| 183 | + """name of the custom action""" |
| 184 | + return "action_updatedtickets" |
| 185 | + def run(self,dispatcher,tracker,domain): |
| 186 | + """action for update ticket in freshdesk""" |
| 187 | + tick=tracker.get_slot("ticket_id") |
| 188 | + prio=tracker.get_slot("priority-up") |
| 189 | + sta=tracker.get_slot("status-up") |
| 190 | + priority_type=int(prio) |
| 191 | + status_code=int(sta) |
| 192 | + data=updateticket(tick,priority_type,status_code) |
| 193 | + if(data.status_code==200): |
| 194 | + dispatcher.utter_message('The ticket is sucessfully updated in freshdesk') |
| 195 | + else: |
| 196 | + dispatcher.utter_message('Something bad happend while ticket updation') |
0 commit comments