I’m trying to build a Discord bot in Python that logs messages and sends them to a service called Hologram.io. I rewrote my code to handle the logging and HTTP request, but when I run it, it throws an error that says:
TypeError: memoryview: a bytes-like object is required, not 'tuple'
I’ve attempted to modify how the JSON payload is formatted, including different adjustments with the API key, but nothing resolves the issue. Below is a revised version of my code snippet:
import urllib.request
import discord
client = discord.Client()
@client.event
async def on_message(message):
body = f"[{message.timestamp:%H:%M}] {message.author}: {message.content}"
values = f'''{{
"deviceid": {12345},
"fromnumber": "{'+1-000-000-0001'}",
"body": "{body}"
}}'''
request = urllib.request.Request(
f'https://api.example.com/sms?key={'your_api_key'}',
data=values,
headers={'Content-Type': 'application/json'}
)
response = urllib.request.urlopen(request).read()
client.run('email', 'password')
What could be causing this error and how can I properly convert my data to a bytes-like object before sending the HTTP request?