Lately, we sometimes get questions like “The code samples on GitHub are written in C#, they don’t seem to convert to VB. Do you have any code sample for visual basic ?”
The good news is that now we have! The project below demonstrates how to use the Cleantalk API with VBScript to validate registrations. Feel free to ask any questions – we’re here to help.
Requirements
- Register a Cleantalk account https://cleantalk.org/register?product=anti-spam
- Obtain the access key from the CleanTalk account https://cleantalk.org/help/add-website
Usage
1. Place the code of Cleantalk class in your VBScript file.
```vbscript
Class CleantalkClass
Private auth_key
Private check_message
Private user_email
Private user_ip
Private user_name
Private user_js_state
Private user_submit_time
Private form_event_token
Private response
Private verdict
Private codes
Private comment
... other class code
... other class code
else
validateResponse = false
end if
end function
end class
```
2. Initialize the Cleantalk class instance, use your own access key when instantiating.
Dim Cleantalk : Set Cleantalk = (New CleantalkClass)("your_access_key", "check_newuser")
3. Set user data when the logic is ready to check the user.
Cleantalk.setUserEmail("st********@*****le.com")
Cleantalk.setUserIP("10.10.10.10")
Cleantalk.setUserName("John Doe")
Cleantalk.setUserJSState("1")
Cleantalk.setUserSubmitTime("5")
Cleantalk.setFormEventToken("a_32_symbols_event_token_value")
4. Send request to the API.
Send request to the API:
5. Validate the response.
If Cleantalk.validateResponse Then
If Cleantalk.getVerdict = 1 Then
WScript.Echo "Validation success. User is allowed."
Else
WScript.Echo "Validation success. User is blocked. Reason: " & Cleantalk.getCodes & " " & Cleantalk.getComment
End If
Else
WScript.Echo "Validation failed. Code: " & Cleantalk.getCodes & " Comment: " & Cleantalk.getComment
End If
Important! Do validation every time after response gathering.
Functions
- setUserEmail(email)
- setUserIP(ip)
- setUserName(name)
- setUserJSState(jsState)
- setUserSubmitTime(submitTime)
- setFormEventToken(eventToken)
- sendRequest()
- validateResponse()
- getVerdict()
- getCodes()
- getComment()
Example
Dim Cleantalk : Set Cleantalk = (New CleantalkClass)("your_auth_key", "check_message")
Cleantalk.setUserEmail("us**@*****le.com")
Cleantalk.setUserIP("192.168.1.1")
Cleantalk.setUserName("John Doe")
Cleantalk.setUserJSState("0")
Cleantalk.setUserSubmitTime("0")
Cleantalk.setFormEventToken("your_event_token")
Cleantalk.sendRequest
If Cleantalk.validateResponse Then
If Cleantalk.getVerdict = 1 Then
WScript.Echo "Validation success. User is allowed."
Else
WScript.Echo "Validation success. User is blocked. Reason: " & Cleantalk.getCodes & " " & Cleantalk.getComment
End If
Else
WScript.Echo "Validation failed. Code: " & Cleantalk.getCodes & " Comment: " & Cleantalk.getComment
End If
Implementing of BotDetector JavaScript library
To use the BotDetector JavaScript library, you need to include the script in the HTML of the page.
<script src="https://moderate.cleantalk.org/ct-bot-detector-wrapper.js"></script>
This script will automatically detect the form submission event and send the data to the Cleantalk API.
Please note, that the script does not perform any checks, just sends the user’s frontend data (like JavaScirpt state, mouse position etc.) to the API.
Example
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Register</title>
<!--Bot-detector JS library wrapper. This script must be added to the HTML of the page.-->
<script src="https://moderate.cleantalk.org/ct-bot-detector-wrapper.js"></script>
</head>
<body>
<form method="post" action="your_form_handler_script">
<label for="user_name">User name</label>
<label for="user_email">User email</label>
<input type="text" name="user_name" id="search_field" /> <br />
<input type="text" name="user_email" id="search_field" /> <br />
<input type="submit" />
</form>
</body>
</html>
When you got added the script, the form will be updated with hidden event_token field after the script loaded. This field value you should transfer to VB Script
Once the token is provided in the API request, the VB Script will make the API takes in count the frontend data.
Make note, that data provided on event_token have the higher priority than the data set by optional VB Script setters.