我正在使用Twilio whatsapp API回复媒体消息。每当有人在whatapp上用图片向我发送消息时,Twilio就会用狗图片用消息“感谢图像”进行响应,如果有人用短信发送了twilio号,则它会用狗图片来响应“向我们发送图像!”。
当我发送短信时,我得到的是一张带有“发送给我们的图像”文字的狗图片,但其他功能不起作用。
我认为该错误是在我正在写入的服务器中。因为即使我遵循他们的official documentation,它仍会继续产生错误,但是会不断出错。
from flask import Flask, request
import requests
from twilio.twiml.messaging_response import MessagingResponse
app = Flask(__name__)
GOOD_BOY_URL = "https://images.unsplash.com/photo-1518717758536-85ae29035b6d?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1350&q=80"
@app.route("/whatsapp", methods=["GET", "POST"])
def reply_whatsapp():
num_media = int(request.values.get("NumMedia"))
response = MessagingResponse()
if not num_media:
msg = response.message("Send us an image!")
else:
msg = response.message("Thanks for the image(s).")
msg.media(GOOD_BOY_URL)
return str(response)
if __name__ == "__main__":
app.run()
错误
Traceback (most recent call last):
File "C:\Users\mnauf\Anaconda3\lib\site-packages\flask\app.py", line 2292, in wsgi_app
response = self.full_dispatch_request()
File "C:\Users\mnauf\Anaconda3\lib\site-packages\flask\app.py", line 1815, in full_dispatch_request
rv = self.handle_user_exception(e)
File "C:\Users\mnauf\Anaconda3\lib\site-packages\flask\app.py", line 1718, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "C:\Users\mnauf\Anaconda3\lib\site-packages\flask\_compat.py", line 35, in reraise
raise value
File "C:\Users\mnauf\Anaconda3\lib\site-packages\flask\app.py", line 1813, in full_dispatch_request
rv = self.dispatch_request()
File "C:\Users\mnauf\Anaconda3\lib\site-packages\flask\app.py", line 1799, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "c:/Users/mnauf/Desktop/IOT/twilio/whatsapp/replyWithMediaToIncomingMsg.py", line 19, in reply_whatsapp
num_media = int(request.values.get("NumMedia"))
TypeError: int() argument must be a string, a bytes-like object or a number, not 'NoneType'
127.0.0.1 - - [11/May/2019 10:05:02] "GET /whatsapp HTTP/1.1" 500 -
[2019-05-11 10:06:07,095] ERROR in app: Exception on /whatsapp [GET]
答案 0 :(得分:0)
如果我的理解正确,那么当您仅发送短信时它可以工作,但是当您发送带有图像的消息时,它就无法工作。
Twilio的教程正在掩盖(模糊)一些代码以强调步骤之间的更改,对我来说,此功能似乎无法正常工作,因此您缺少了一些代码(用于处理传入的媒体附件的部分)。
下面是完整的代码,您需要创建一个app_data
文件夹来存储传入的媒体文件。
import mimetypes
import os
from urllib.parse import urlparse
from flask import Flask, request
import requests
from twilio.twiml.messaging_response import MessagingResponse
app = Flask(__name__)
GOOD_BOY_URL = "https://images.unsplash.com/" \
"photo-1518717758536-85ae29035b6d?" \
"ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1350&q=80"
@app.route("/whatsapp", methods=["GET", "POST"])
def reply_whatsapp():
num_media = int(request.values.get("NumMedia"))
media_files = []
for idx in range(num_media):
media_url = request.values.get(f'MediaUrl{idx}')
mime_type = request.values.get(f'MediaContentType{idx}')
media_files.append((media_url, mime_type))
req = requests.get(media_url)
file_extension = mimetypes.guess_extension(mime_type)
media_sid = os.path.basename(urlparse(media_url).path)
with open(f"app_data/{media_sid}{file_extension}", 'wb') as f:
f.write(req.content)
response = MessagingResponse()
if not num_media:
msg = response.message("Send us an image!")
else:
msg = response.message("Thanks for the image(s).")
msg.media(GOOD_BOY_URL)
return str(response)
if __name__ == "__main__":
app.run()