This is a simple Cloudflare worker script that can submitt html form data with help of Cloudflare worker and Telegram bot api.
To run this project, you will need a Cloudflare account and have to add the following environment variables to your Cloudflare workers settings.
TELEGRAM_API_KEY
= "You get it from @Botfather"
TELEGRAM_CHAT_ID
= "Your telegram Group id you get using @MissRose_bot "
Open Botfather and create a new bot and you will get an API key keep it safe to use later
Create a Group and add your newly created bot in the Group and give admin permission to it.(Its important to give admin permission to receive messages)
You have to figure out your Group id, it can be done by adding another popular bot in your Group which is MissRose_bot https://t.me/MissRose_bot
Give it admin permission and send /id
in the group. You will get your group id which looks something like this -10069696969669
First of all login to cloudflare and make an account if you don't have one.
Head towards Workers & Pages section and Create a new worker
Give a name to your worker, it will be the final link that will be necessary later.
click on Continue to project and head towards Setting > Variables > Add Variables.
And add the following Variables
TELEGRAM_API_KEY
= "You get it from @Botfather"
TELEGRAM_CHAT_ID
= "Your telegram Group id you get using @MissRose_bot "
and press "Deploy".
Click on the Edit Code button located at the top rught corner. Copy the code from this repositories worker.js file and paste it there in cloudflare.
"Its locate at the 6th line"
async function handleRequest(request) {
const allowedOrigin = 'valid_url_here'; //your url goes here
it is the url of your website from where you wanna allow to take html form data. This will serve as the authorised website where this thing will be allowed to use.
Open the worker url and keep it safe for later use.
Use the worker link in your website, I have given a simple format in the repo as form.txt
Html use case is farely simple just replace the POST
action
url to cloudflare workers url
I have given the minimum code for demonstration purposes please feel free to use your creativity.
Here is the code you need to replace the https://yourcloudflare.workers.dev/
to your workers url.
<form id="contactForm" action="https://yourcloudflare.workers.dev/" method="POST">
<label for="name">Name:</label>
<input type="text" id="name" name="name" required>
<br>
<label for="Phone">Phone No:</label>
<input type="number" id="Phone" name="Phone" required>
<br/>
<label for="subject">Subject:</label>
<input type="text" id="subject" name="Subject" required>
<br>
<!-- <label for="options">Options:</label>
<select id="options" name="options" required>
<option value="" disabled selected>Select an option</option>
<option value="option1">cause one</option>
<option value="option2">cause 2</option>
<!-- Add more options as needed
</select>-->