Self-hosted contact server for receiving telegram messages. Install it, configure it, create a secret key and start receiving messages.
Most used case when you have a site with a contact form and want receive messages in telegram.
Clone the project
git clone git@github.com:dsabre/contact-server.gitGo to the project directory
cd contact-serverInstall dependencies
pnpm installStart development server
pnpm devStart production server
pnpm startTo run this project, you will need to add the following environment variables to your host server (inside a .env.local file in project root).
You can use pnpm set-config command to interactively create a configuration file.
Example of .env.local file:
# Port where the program will listen
PORT=...
# Telegram bot token
BOT_TOKEN=...
# Telegram chat id where the bot will send messages
CHAT_ID=...
# allowed origin
CORS_ORIGIN=...
# secret passphrase for encode and decode secret keys (see secret key generation section for further details)
CRYPTO_SECRET=...
# google recaptcha secret, used for server-side request validation
RECAPTCHA_SECRET=...
# enable or disable logging
LOG_ENABLED="true|false" POST /send-message| Parameter | Type | Description |
|---|---|---|
message |
string |
Required. The message to send via telegram |
secretKey |
string |
Required. Your encoded secret key |
grecaptchaToken |
string |
Required if RECAPTCHA_SECRET is not empty. Token returned from a Google reCAPTCHA challenge |
title |
string |
The contact request title, if not provided will be used "CONTACT REQUEST!" |
extraData |
object |
Any extra data you want to send on Telegram message |
// the contact server url to call, change it to match your requirements
const sendMessageUrl = 'http://localhost:3000/send-message';
// message to send via Telegram, usually this will be passed via form
const message = 'Hello!!';
// set here any extra data you want to send on Telegram message, here is an example of what I used for my personal site
const extraData = {
Name: 'John Doe',
Locale: 'en',
Site_theme: 'dark' // <-- underscores will be replaced by spaces
};
// Axios example, but you can use what you want:
axios.post(sendMessageUrl, {
message: message.trim(),
grecaptchaToken: 'TOKEN RETURNED FROM A GOOGLE RECAPTCHA CHALLENGE',
secretKey: 'YOUR ENCODED SECRET KEY',
extraData: extraData
}).then(() => {
console.log('success');
}).catch(() => {
console.error('error!');
});Before to launch this, write a sample message to the bot.
pnpm get-chat-idspnpm get-secret-key
