I have set up a bot for Discord that is taking every users' messages and using a sentiment classifier to determine if a message is positive or negative. See the POST request to the text-processing.com API below.
Given a message 'I am extremely happy', the API will return a response which is a string resembling JSON:
{"probability": {"neg": 0.32404484915164478, "neutral": 0.021768879244280313, "pos": 0.67595515084835522}, "label": "pos"}
How can I convert this JSON object to a str, so that I can easily interact with its data?
@bot.event
async def on_message(message):
if message.author == bot.user:
return
with open('data.txt', 'a') as f:
print(repr(message.content), file=f)
response = requests.post('http://text-processing.com/api/sentiment/', {
'text': message.content
}).json()
print(response, file=f)
d = json.loads(response)
print(d["probability"]["pos"], file=f)
f.close()
await bot.process_commands(message)
The error code I am receiving is...
File "/Users/enzoromano/PycharmProjects/NewDiscordBot/NewBot.py", line 44, in on_message
d = json.loads(response)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/json/__init__.py", line 348, in loads
'not {!r}'.format(s.__class__.__name__))
TypeError: the JSON object must be str, bytes or bytearray, not 'dict'
s['probability']['pos']assuming your dictionary above is assigned tos