问题描述
我参考了 Mwangi Kabiru 发布的 Twilio 博客:
https://www.twilio.com/blog/serverless-whatsapp-chatbot-python-google-cloud-functions-twilio
我对代码进行了必要的更改,以便从 Google 云端硬盘中提取 Google 表格数据,并通过网络钩子将其发送到 Twilio 聊天机器人(自动驾驶仪)。根据 Google Cloud Function 日志,webhook 根据其请求成功将信息传输到 Twilio 聊天机器人(自动驾驶仪)。但是,Twilio 正在抛出“错误 - 90100”:
无效的 Autopilot Actions JSON:无效的 Autopilot Action 可能的原因 Actions JSON 不符合 Actions Schema (https://carnelian-neanderthal-8008.twil.io/assets/ActionsSchema.json)
可能的解决方案 根据操作架构 (https://carnelian-neanderthal-8008.twil.io/assets/ActionsSchema.json) 测试您的 JSON 响应
如何下载在 Google Cloud Function 上创建的 webhook 的 JSON 文件并针对 Twilio Actions Schema 对其进行测试?有人可以帮我解决这个问题吗?
import gspread
from oauth2client.service_account import ServiceAccountCredentials
from datetime import datetime
import requests
from twilio.twiml.messaging_response import MessagingResponse
def whatsapp_webhook(request):
'''
HTTP Cloud Function.
Parameters
----------
request (flask.Request) : The request object.
<https://flask.palletsprojects.com/en/1.1.x/api/#incoming-request-data>
Returns
-------
The response text,or any set of values that can be turned into a
Response object using `make_response`
<https://flask.palletsprojects.com/en/1.1.x/api/#flask.make_response>.
'''
# google drive json dictionary
data = {
#json details generated by Google Cloud Platform
}
# use creds to create a client to interact with the Google Drive API
scope = ['https://spreadsheets.google.com/Feeds','https://www.googleapis.com/auth/drive']
creds = ServiceAccountCredentials.from_json_keyfile_dict(data,scope)
client = gspread.authorize(creds)
# Find a workbook by name and open the first sheet
# Make sure you use the right name here.
wb = client.open("Covid_DB")
sheet = wb.worksheet('Oxygen')
#Checking whether Google Cloud is able to access Google Sheets
row = ['Google Sheet Accessed successfully',':)']
index = 9
sheet.insert_row(row,index)
#Sending the required info to the user
twilio_response = MessagingResponse()
msg = twilio_response.message()
msg.body('1) ' + ','.join(sheet.row_values(2)) + ' 2) ' + ','.join(sheet.row_values(3)))
return str(twilio_response)
Twilio's-Task-Option-1-Request
解决方法
当您通过 @Configuration
@EnableWebSecurity
public class SecurityConfiguration extends WebSecurityConfigurerAdapter{
@Autowired
private UserService userService;
@Bean
public BCryptPasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
@Bean
public DaoAuthenticationProvider authenticationProvider() {
DaoAuthenticationProvider auth = new DaoAuthenticationProvider();
auth.setUserDetailsService(userService);
auth.setPasswordEncoder(passwordEncoder());
return auth;
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.authenticationProvider(authenticationProvider());
}
//.antMatchers("/","/home**").anonymous()
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/","/home","/cars","/motors**").anonymous()
.antMatchers("/","/motors**").permitAll()
.antMatchers("/cars/add").authenticated()
.and()
.formLogin()
.loginPage("/login")
.permitAll()
.defaultSuccessUrl("/")
.and()
.logout()
.invalidateHttpSession(true)
.clearAuthentication(true)
.logoutRequestMatcher(new AntPathRequestMatcher("/logout"))
.logoutSuccessUrl("/login?logout")
.permitAll();
}
从 Twilio Autopilot 调用 URL 时,您需要为 Twilio Autopilot 而不是 TwiML 返回 JSON。
您需要更改构建返回消息的部分:
redirect
在此处返回 JSON:
twilio_response = MessagingResponse()
msg = twilio_response.message()
msg.body('1) ' + ','.join(sheet.row_values(2)) + ' 2) ' + ','.join(sheet.row_values(3)))
return str(twilio_response)
有关 Twilio Autopilot 支持的操作列表,请参阅“Actions Overview”。