keiffster / program-y

Python 3.x based AIML 2.0 Chatbot interpreter, framework, related programs and knowledge files
https://keiffster.github.io/program-y/
Other
349 stars 137 forks source link

Adding key in response json of my own service #278

Closed Yasminabouzbiba closed 3 years ago

Yasminabouzbiba commented 3 years ago

Issue Tracker can be used for report issues and new features. All requests will be accepted, reviewed and responded to. However submitting a request for a new feature does not guarantee that it will be implemented, but we will commit to responded with 7 working days if a new feature will be added to the backlog.

You can see the full backlog in Trello at Programy Backlog

Expected Behavior

Current Behavior

Possible Solution

Steps to Reproduce

1. 2. 3. 4.

Context (Environment)

Detailed Description

Possible Implementation

Yasminabouzbiba commented 3 years ago

Hi, I have created my own service. In the ask_question function we return only the response of the question (sent by my bot) in key "text" but I would like to add a key to the response json. The response json looks like : { "meta": { "authors": [ "Keith Sterling" ], "botName": "Y-Bot", "copyright": "Copyright 2016-2019 keithsterling.com", "version": "3.9" }, "response": { "query": "xxxxxxxxxxxxxxxxxxxxxx", "text": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", "timestamp": 1607500351.695998, "userId": "12" }, "status": { "code": 200, "message": "success" } } I would like to add a key "contextId" returned from my bot to keep memory of same conversation. How can I do that ? Hope I have been clear :)

Thank you in advance for your response.

keiffster commented 3 years ago

Would it not be better to just add another json field to the payload, when you get the response back in JSON from y-bot, just add another field

response = y-bot.ask_question()

response[‘key’] = “Your Key Text Here”

return response

On 9 Dec 2020, at 08:06, Yasminabouzbiba notifications@github.com wrote:

Hi, I have created my own service. In the ask_question function we return only the response of the question (sent by my bot) in key "text" but I would like to add a key to the response json. The response json looks like : { "meta": { "authors": [ "Keith Sterling" ], "botName": "Y-Bot", "copyright": "Copyright 2016-2019 keithsterling.com", "version": "3.9" }, "response": { "query": "xxxxxxxxxxxxxxxxxxxxxx", "text": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", "timestamp": 1607500351.695998, "userId": "12" }, "status": { "code": 200, "message": "success" } } I would like to add a key "contextId" returned from my bot to keep memory of same conversation. How can I do that ? Hope I have been clear :)

Thank you in advance for your response.

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/keiffster/program-y/issues/278#issuecomment-741605865, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABCUFM3AD4B4EYSKUEZNHPTST4VYTANCNFSM4UTDJQNQ.

Yasminabouzbiba commented 3 years ago

My service looks like this (in programy/services) Where can I add this line so I can add a field in my response json. Or do I need to modify a file in ybot ?

`from programy.utils.logging.ylogger import YLogger from programy.services.service import Service from programy.services.dydurequestapi import DyDuRequestsAPI import base64

class DyDuAPI:

def __init__(self, request_api=None):
    if request_api is None:
        self._requests_api = DyDuRequestsAPI()
    else:
        self._requests_api = request_api

def ask_question(self, url, question, space, language):
    payload = {'userInput': question, "space": space, "language": language}
    response = self._requests_api.post(url, data=payload)

    if response is None:
        raise Exception("No response from DyDu service")

    if response.status_code != 200:
        raise Exception("Error response from DyDu service [%d]" % response.status_code)

    json_data = response.json()
    return base64.b64decode(json_data["values"]["text"]).decode("utf-8")

class DyDuService(Service):

def __init__(self, config=None, api=None):
    Service.__init__(self, config)

    if api is None:
        self._api = DyDuAPI()
    else:
        self._api = api

    self._url = None
    if config.url is None:
        raise Exception("Undefined url parameter")
    else:
        self._url = config.url

def ask_question(self, client_context, question: str):
    try:
        return self._api.ask_question(self._url, question, "default", "French")
    except Exception as e:
        YLogger.error(client_context, "General error querying DyDu for question [%s] - [%s]", question,
                      str(e))
    return ""

`

Yasminabouzbiba commented 3 years ago

Actually I have two Api :

However for the moment my service works well but only for new conversations. Now, I am trying to find a way to handle context of a conversation. That's why I need to get the contextId sent by my API so I can use it and call the other API. Does that make sense ? :)

keiffster commented 3 years ago

Is this still an issue for you ?

keiffster commented 3 years ago

You will need to modify the code to add additional parameters. The response doesn include contextId, does this not work

Yasminabouzbiba commented 3 years ago

Hello, yes we found a solution to do this thank you !