我一直在编写以下Python 3代码:
import urllib.request
import discord
import asyncio
#Colors
black = "\033[0;30m"
red = "\033[0;31m"
green = "\033[0;32m"
yellow = "\033[0;33m"
blue = "\033[0;34m"
purple = "\033[0;35m"
cyan = "\033[0;36m"
white = "\033[0;37m"
#Bold Colors
bblack = "\033[1;30m"
bred = "\033[1;31m"
bgreen = "\033[1;32m"
byellow = "\033[1;33m"
bblue = "\033[1;34m"
bpurple = "\033[1;35m"
bcyan = "\033[1;36m"
bwhite = "\033[1;37m"
print(""""""+blue+""" ____ _ _ """+red+""" _____
"""+blue+""" | _ \(_)___ ___ ___ _ __ __| | """+red+"""|__ /___ _ __ ___
"""+blue+""" | | | | / __|/ __/ _ \| '__/ _` | """+red+""" / // _ \ '__/ _ \
"""+blue+""" | |_| | \__ \ (_| (_) | | | (_| | """+red+""" / /| __/ | | (_) |
"""+blue+""" |____/|_|___/\___\___/|_| \__,_|"""+red+""" /____\___|_| \___/
""")
yes = ['y','Y','yes','Yes','YES']
email = input(bgreen+'Discord Email Address: '+yellow)
password = input(bgreen+'Discord Password: '+yellow)
hg_api = str(input(bcyan+'Hologram.io API Key: '+yellow))
hg_deviceid = int(input(bcyan+'Hologram.io Device ID: '+yellow))
sender = input(bcyan+'Sender Phone number (leave blank for default: +1-000-000-0001): '+yellow)
if sender == '':
sender = "+1-000-000-0001"
logs = input(bpurple+'Enable console logs [Y/N]: '+yellow)
print(white)
client = discord.Client()
@client.event
async def on_ready():
print(green+'Logged in as: '+cyan+client.user.name+white+' '+yellow+'('+client.user.id+')'+white)
@client.event
async def on_message(message):
author = str(message.author)
if logs in yes:
print(yellow+"["+str(message.timestamp)[11:16]+"] "+red+author+": "+cyan+message.content+white)
if message.author.id != client.user.id:
body = '['+str(message.timestamp)[11:16]+'] '+author+': '+message.content
body = str(body)
print(body)
values = """
{
"deviceid": """,hg_deviceid,""",
"fromnumber": """,sender,""",
"body": """,body,"""
}
"""
headers = {
'Content-Type': 'application/json'
}
#try:
request = urllib.request.Request('https://dashboard.hologram.io/api/1/sms/incoming?apikey='+hg_api, data=values, headers=headers)
response_body = urllib.request.urlopen(request).read()
#except:
# print(red+"Failed to login. Message not sent."+white)
client.run(email, password)
当我运行它时,我收到此错误:
Logged in as: Coto (234246004424179712)
[21:01] EmojiGuy#7257: Either spoopy the bot is dead or it has a check then I guess
[21:01] EmojiGuy#7257: Either spoopy the bot is dead or it has a check then I guess
Ignoring exception in on_message
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/urllib/request.py", line 1190, in do_request_
mv = memoryview(data)
TypeError: memoryview: a bytes-like object is required, not 'tuple'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/discord/client.py", line 307, in _run_event
yield from getattr(self, event)(*args, **kwargs)
File "DiscordZero.py", line 73, in on_message
response_body = urllib.request.urlopen(request).read()
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/urllib/request.py", line 163, in urlopen
return opener.open(url, data, timeout)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/urllib/request.py", line 464, in open
req = meth(req)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/urllib/request.py", line 1195, in do_request_
data))
ValueError: Content-Length should be specified for iterable data of type <class 'tuple'> ('\n\t\t {\n\t\t "deviceid": ', 108749, ',\n\t\t "fromnumber": ', '+1-000-000-0001', ',\n\t\t "body": ', '[21:01] EmojiGuy#7257: Either spoopy the bot is dead or it has a check then I guess', '\n\t\t }\n\t\t')
有人可以解释那是什么吗?我尝试过删除用+hg_api
取代,hg_api
等内容,但没有任何作用......
我不明白什么是错的。我已经搜索了错误消息和所有内容,似乎无法找到解决方法。
(使用Discord Python API&amp; hologram.io API)
答案 0 :(得分:3)
values
是一个元组,而不是一个字符串:
values = """
{
"deviceid": """,hg_deviceid,""",
"fromnumber": """,sender,""",
"body": """,body,"""
}
"""
这是一系列字符串,以逗号分隔。
不是手动尝试构造JSON字符串,而是使用json
库为您编码字典。添加
import json
导入并使用它来生成values
:
values = json.dumps({
"deviceid": hg_deviceid,
"fromnumber": sender,
"body": body,
}).encode('utf8')