import { Component } from "@angular/core";
import { HttpClient } from "@angular/common/http";
import Botpoison from "@botpoison/browser";
@Component({
selector: "app-contact-form",
template: `
<form (ngSubmit)="onSubmit()">
<textarea [(ngModel)]="message" name="message" placeholder="Enter your message"></textarea>
<button type="submit" [disabled]="loading">Send</button>
</form>
`,
})
export class ContactFormComponent {
message = "";
loading = false;
private botpoison = new Botpoison({
publicKey: "pk_xxxxxxxx",
});
constructor(private http: HttpClient) {}
async onSubmit() {
this.loading = true;
try {
const { solution } = await this.botpoison.challenge();
await this.http
.post("https://example.demo/", {
message: this.message,
_botpoison: solution,
})
.toPromise();
alert("Form submitted");
this.message = "";
} catch (error) {
alert("Error submitting form");
} finally {
this.loading = false;
}
}
}