[英]Got an error in twilio + flask + python + whatsapp automation
我正在嘗試使用 python 和燒瓶進行 Whatsapp 自動化,當我在 Whatsapp 中向 Twilio 發送消息時遇到一些錯誤。 我的代碼是這樣的
from flask import Flask,request
from twilio.twiml.messaging_response import MessagingResponse
import io
import datetime
#import xmlrpc.client import strftime
appbot=Flask(__name__)
@appbot.route("/sms",methods=["get","post"])
def reply():
with io.open("response.csv","a",encoding="utf-8")as f1:
ur = request.form.get("Body")
un = request.form.get("From")
print(un)
print(ur)
un = un.replace("whatsapp:","")
dt=datetime.datetime.now().strftime("%y%m%d--%H%M%S")
data = un+","+ur+","+dt
f1.write(data)
resp = MessagingResponse("You sent"+" "+ur+"from"+" "+un+"on"+" "+dt)
return(str(resp))
f1.close()
if (__name__ == "__main__"):
appbot.run()
首先,我還必須更改 Twilio 中的鏈接,但我仍然遇到錯誤。
[2020-08-23 13:23:19,609] ERROR in app: Exception on /sms [POST]
Traceback (most recent call last):
File "C:\Users\KENIL\Anaconda3\lib\site-packages\flask\app.py", line 2446, in wsgi_app
response = self.full_dispatch_request()
File "C:\Users\KENIL\Anaconda3\lib\site-packages\flask\app.py", line 1951, in full_dispatch_request
rv = self.handle_user_exception(e)
File "C:\Users\KENIL\Anaconda3\lib\site-packages\flask\app.py", line 1820, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "C:\Users\KENIL\Anaconda3\lib\site-packages\flask\_compat.py", line 39, in reraise
raise value
File "C:\Users\KENIL\Anaconda3\lib\site-packages\flask\app.py", line 1949, in full_dispatch_request
rv = self.dispatch_request()
File "C:\Users\KENIL\Anaconda3\lib\site-packages\flask\app.py", line 1935, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "G:/P P SAVANI SCHOOL OF ENGINEERING/4 Forth year/Others/Chatbot/udemychat/app.py", line 27, in reply
resp = MessagingResponse("You sent"+" "+ur+"from"+" "+un+"on"+" "+dt)
TypeError: __init__() takes 1 positional argument but 2 were given
127.0.0.1 - - [23/Aug/2020 13:23:19] "POST /sms HTTP/1.1" 500 -
您需要實例化一個響應並在其上調用.message
方法。
來自文檔: https : //www.twilio.com/docs/sms/tutorials/how-to-receive-and-reply-python
@app.route("/sms", methods=['GET', 'POST'])
def sms_reply():
"""Respond to incoming calls with a simple text message."""
# Start our TwiML response
resp = MessagingResponse()
# Add a message
resp.message("The Robots are coming! Head for the hills!")
你得到的錯誤來自一個類的所有方法(包括__init__
)都傳入self
,這就是為什么你傳入的消息成為第二個參數並且 Python 拋出TypeError: __init__() takes 1 positional argument but 2 were given
錯誤
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.