假设你有一个简单的应用程序,在 https:/laland.shinyapps.iomail-form。 的功能,作为一个联系表单。在发送邮件之前,你要测试发件人是不是一个机器人。这可能看起来像这样。
global:
library(shiny)
library(shinyAce)
library(mailR)
: ui.R:
ui<-shinyUI(
fluidPage(
fluidRow(
column(2
,textInput("contact_name", "Name*", placeholder = "Ed Snow")
),
column(2, offset = 0
,textInput("contact_email", "Email*", placeholder = "[email protected]")
)
),
fluidRow(
column(4,
aceEditor(outputId = "contact_message", value = "...", fontSize = 13)
)
),
fluidRow(
column(2,
checkboxInput("contact_not_a_robot", "I'm not a robot*", value = FALSE), # !!! <---
actionButton("contact_click_send", "Send")
))
)
)
服务器.R:
server <- shinyServer(function(session,input, output) {
observeEvent(input$contact_click_send, {
if( is.null(input$contact_click_send) || input$contact_click_send==0
|| !input$contact_not_a_robot){ # !!! <---
return(NULL)
}
send.mail(from = "[email protected]",
to = "[email protected]",
subject = "Shower time!",
body = input$contact_message,
smtp = list(host.name = "smtp.gmail.com"
, port = 465
, user.name = "[email protected]"
, passwd = "DONALD_BIG_HANDS123"
, ssl = TRUE),
authenticate = TRUE,
html = TRUE, send = TRUE)
# reset form
updateTextInput(session, "contact_name", value = "")
updateTextInput(session, "contact_email", value = "")
updateAceEditor(session, "contact_message", value = "Message sent succesfully!")
updateCheckboxInput(session, "contact_not_a_robot", value = FALSE)
updateActionButton(session, "contact_click_send", icon = icon("check"))
})
})
这个问题换个说法:如何在这个RShiny联系表单中编织(重新)验证码?
对于reCAPTCHAv3,请使用 https:/github.comsarthi2395shinygCAPTCHAv3。
devtools::install_github("sarthi2395/shinygCAPTCHAv3")
把这个这个添加到你的 Global.R:
library(shinygCAPTCHAv3)
ui.R:
ui<-shinyUI(
fluidPage(
GreCAPTCHAv3Ui(<your site key>,"homepage","responseReceived"),
#Rest of your code
)
)
在这里你必须输入你的 网站关键词 在reCAPTCHA管理控制台为你注册的域名生成。在这里,我已经指定了 行动 作为'主页',但你可以选择一个适合你的目的(https:/developers.google.comrecaptchadocsv3).
响应收到 是我们将在这里使用的对象,将收到的令牌传递给shiny服务器,以便与Google进行验证。
服务器.R
server <- shinyServer(function(session,input, output) {
observeEvent(input$responseReceived, {
result <- GreCAPTCHAv3Server(<your secret key>,input$responseReceived)
if(result$success){
#Rest of your server logic.
}
})
})
在这里你必须输入你的 秘钥 在reCAPTCHA管理控制台为您注册的域名生成的。
如果一切顺利,当你启动网页时,你会看到右下角的reCAPTCHA框。希望能帮到你。