0

I am trying to make a chat bot where the intents.json(bot.json for my case) is loaded on a website I can access it using the "urlopen(url)" function with urllib but I can't dump anything into that json file here is the file

import random
import json
from urllib.request import urlopen

url = "https://proj002webhostingserver.husseingaming1.repl.co/bot.json"
web = urlopen(url)


def random_string(question):
    random_list = [
        "Please try writing something more descriptive. Can You Please Respond With The Answer",
        "Oh! It appears you wrote something I don't understand yet, Can You Please Respond With The Answer",
        "Do you mind trying to rephrase that? Can You Please Respond With The Answer",
        "I'm terribly sorry, I didn't quite catch that. Can You Please Respond With The Answer",
        "I can't answer that yet, please try asking something else. Can You Please Respond With The Answer"
    ]

    list_count = len(random_list)
    random_item = random.randrange(list_count)
    web = urlopen(url)
    response_data = load_json(web)
    print(random_list[random_item])
    respinp = input("Type Your Response To That Question: ")
    reqwinp = list(input("Type Any Requierd Words Here(Leave empty if there are none)(use this format: [\"word1\", \"word 2\"] you can use more than 2 or less than 2): "))
    new(question=question, response= respinp, required_words= reqwinp)


def load_json(file):
    with urlopen(url) as bot_responses:
        print(f"Loaded \"{file}\" successfully!")
        return json.load(bot_responses)
    

def new(question: str, response: str, required_words: None and str):
    """_summary_

    Args:
        question (str): The question that you asked
        response (str): An answer to the question
        required_words (Noneandstr): any requierd words for this question(leave empty if there are no required words)
    """
    data = load_json(web)
    data.append({
    "response_type": "added",
    "user_input": [question],
    "bot_response": response,
    "required_words": [required_words]
                })
    with  urlopen(url, "w") as file:
        json.dumps(data, file)
random_string("hi")

the fuction where I dump my dictionary is the "new" function at the bottom of the code

I tried to run the code but it gave me this error

Exception has occurred: TypeError
POST data should be bytes, an iterable of bytes, or a file object. It cannot be of type str.
  File "/Users/hussein/Desktop/Chatbot_proj_Callback001_version1/random_responses.py", line 49, in new
    with  urlopen(url, "w") as file:
          ^^^^^^^^^^^^^^^^^
  File "/Users/hussein/Desktop/Chatbot_proj_Callback001_version1/random_responses.py", line 25, in random_string
    new(question=question, response= respinp, required_words= reqwinp)
  File "/Users/hussein/Desktop/Chatbot_proj_Callback001_version1/random_responses.py", line 51, in <module>
    random_string("hi")
TypeError: POST data should be bytes, an iterable of bytes, or a file object. It cannot be of type str.

1 Answers1

0

with urlopen(url, "w") as file:

The second argument of urlopen is data according to the documentation https://docs.python.org/3/library/urllib.request.html#urllib.request.urlopen

Which is supposed to be the data you want to post, not the analogue of mode in open. Bytes or file as said in the traceback.

I believe this answer will help you write your data correctly https://stackoverflow.com/a/36485152/22052558

Maria K
  • 1,491
  • 1
  • 3
  • 14
  • the code works the only issue is that when I check on the server it doesn't seem to have added the data(I checked if the response code was 200 and it was) but the data still doesn't get added do you have anyway to fix that? – Nothing Around Us Jun 26 '23 at 14:49