Home > Web Front-end > JS Tutorial > body text

Contact form and CAPTCHA backend in Open Source Cloud

Mary-Kate Olsen
Release: 2024-10-04 22:26:02
Original
611 people have browsed it

With an open source backend service for form collection and CAPTCHA handling you can implement a contact form with spam protection without developing your own backend services for this. These backend services are also available in Open Source Cloud so you don't have to host these backend services yourself either.

In this blog I will describe how you can quickly get a contact form where the message is sent to a Slack channel.

Contact form and CAPTCHA backend in Open Source Cloud

Create a Slack Bot

The Slack Bot will be the one posting the message to the channel in Slack. Visit https://api.slack.com/apps/ and create a new app with the permissions to post to the desired Slack channel.

Save the Slack Bot token as this will be used later in this tutorial.

Create form backend service in Open Source Cloud

Login to or signup for an account at Open Source Cloud.

Navigate to the Contact Form Service and click on the tab Service Secrets. Click on New Secret and add a secret that contains the Slack Bot token obtained earlier.

Contact form and CAPTCHA backend in Open Source Cloud

Create a contact form service by pressing the Create service button.

Contact form and CAPTCHA backend in Open Source Cloud

Give the service a name and select slack in the transport dropdown. Provide the service secret holding the token and enter the ID of the channel where the message should be posted.

Contact form and CAPTCHA backend in Open Source Cloud

When the service is up and running you can copy and save the URL to the contact form service.

Contact form example in React

Develop your contact form in your frontend application which in React could look something like this. Replace BACKEND-SERVICE-URL with the URL acquired above.


'use client';
import { Input, Textarea } from '@nextui-org/react';

export default function Page() {
  const handleSubmit = (formData: any) => {
    fetch(new URL('BACKEND-SERVICE-URL/contact'), {
      method: 'POST',
      headers: {
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      body: new URLSearchParams(formData).toString()
    });
  };

  return (
    
Copy after login
) }

When the form is submitted you should now get a message in your Slack channel. To prevent spam-bots to misuse this form we need to add a CAPTCHA. CAPTCHA is an acronym that stands for "Completely Automated Public Turing test to tell Computers and Humans Apart."

To provide this functionality we will use an open source CAPTCHA backend service to generate and verify CAPTCHA. How it works is that you generate a CAPTCHA image that displays a text. Then you ask the user (human) to provide the text that he or she sees and the backend will verify that this was the text shown in the image.

Create CAPTCHA backend service in Open Source Cloud

Navigate to the CAPTCHA Service and click on the button Create service.

Contact form and CAPTCHA backend in Open Source Cloud

Give the service a name and once the service has started copy the URL to the service. Replace CAPTCHA-SVC-URL below with this URL.

Add CAPTCHA verification to your form

Extend the form we created above with the following code.


<p>'use client';<br>
import { Input, Textarea } from '@nextui-org/react'; </p>

<p>export default function Page() {<br>
  const [captchaSvg, setContact form and CAPTCHA backend in Open Source CloudSvg] = useState<string null>(null);<br>
  const [captchaId, setContact form and CAPTCHA backend in Open Source CloudId] = useState('');<br>
  const [captcha, setContact form and CAPTCHA backend in Open Source Cloud] = useState('');<br>
  const [invalidContact form and CAPTCHA backend in Open Source Cloud, setInvalidContact form and CAPTCHA backend in Open Source Cloud] = useState(true);</string></p>

<p>useEffect(() => {<br>
    fetch(new URL('/captcha', 'CAPTCHA-SVC-URL'))<br>
      .then((response) => response.json())<br>
      .then((data) => {<br>
        setContact form and CAPTCHA backend in Open Source CloudSvg(data.svg);<br>
        setContact form and CAPTCHA backend in Open Source CloudId(data.id);<br>
      });<br>
  }, []);</p>

<p>const onContact form and CAPTCHA backend in Open Source CloudChange = (value: string) => {<br>
    setContact form and CAPTCHA backend in Open Source Cloud(value);<br>
    fetch(new URL('CAPTCHA-SVC-URL/verify/' + captchaId + '/' + value))<br>
      .then((response) => {<br>
        setInvalidContact form and CAPTCHA backend in Open Source Cloud(!response.ok);<br>
      })<br>
      .catch(() => {<br>
        setInvalidContact form and CAPTCHA backend in Open Source Cloud(true);<br>
      });<br>
  };</p>

<p>const handleSubmit = (formData: any) => {<br>
    fetch(new URL('BACKEND-SERVICE-URL/contact'), {<br>
      method: 'POST',<br>
      headers: {<br>
        'Content-Type': 'application/x-www-form-urlencoded'<br>
      },<br>
      body: new URLSearchParams(formData).toString()<br>
    });<br>
  };</p>

<p>return (<br>
    </p>
Copy after login





)
}




Conclusion

This was an example on how you can add to your web application a contact form with CAPTCHA verification that posts to a Slack channel without having to build or host your own backend services for it.

What is Open Source Cloud?

Open Source Cloud reduces the barrier to get started using open source and reduces the barrier for creators to make their software available as a service. Read more about why we developed it in this post.

The above is the detailed content of Contact form and CAPTCHA backend in Open Source Cloud. For more information, please follow other related articles on the PHP Chinese website!

source:dev.to
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template