Create serverless Telegram Bot with DB to keep track of events in your WooCommerce store using Netlify Functions and FaunaDB
- Getting Telegram Bot token and FaunaDB key
- Deploy to Netlify
- Hooking up the Bot
On Telegram, create a new Telegram bot by talking to @botfather and thereafter getting the token for the Telegram Bot.
On the FaunaDB website, create a database and head to the security section to generate a FaunaDB key for the database.
In your store create a new REST API Key in the WC settings and copy the consumer key + consumer secret.
Deploy to Netlify either by linking to this repository manually or by clicking the Deploy to netlify.
Thereafter, fill in the environment variables
TELEGRAM_BOT_TOKENFAUNA_SECRET_KEYWC_CONSUMER_KEYWC_CONSUMER_SECRETWC_SITE_URL
Once the above are done, you need to specify and tell telegram where your bot should direct the messages it received to. Do so by simply visiting this url
https://{your_netlify_domain}/api/updateWebhooks
This will notify telegram and updates the webhook. It also creates or updates the necessary webhooks in your shop.
If you want to update the webhooks after a successful deploy automatically, go to your Netlify site settings > Build & deploy and create a new 'Outgoing webhook' notification. Select 'Deploy succeeded' and enter the URL to your updateWebhooks function.
Afterwhich, YOU ARE DONE!🎉🎉
Try typing /start to your bot and see if it works. If everything worked, it'll greet you and tell you that you as the first user are the administrator.
If you want to set the command list write /setcommands to the bot father and send him this list:
start - Start the bot
chatid - Display current chat id
enablenotifications - Enable shop notifications for current chat
disablenotifications - Disable shop notifications for current chat
/start: Add own user to the database. If first user, role is set to administrator./chatId: Print chat ID of current chat./enableNotifications: Enable WooCommerce notification (e.g. new orders) for current chat./disableNotifications: Disable WooCommerce notification for current chat.
This bot uses telegraf.js for its Telegram Bot functionality. Head to its website to find out how to extend the functionality of the bot you have created :)
To locally test and develop the bot follow these steps:
- Create a new Telegram Bot and a new FaunaDB database for testing.
- Create
.envfile and enter your bot token + secret key:TELEGRAM_BOT_TOKEN=your_dev_bot_token FAUNA_SECRET_KEY=your_dev_secret_key WC_CONSUMER_KEY=your_dev_consumer_key WC_CONSUMER_SECRET=your_dev_consumer_secret WC_SITE_URL=https://www.example.com - Setup Netlify CLI for Netlify Dev server (more info)
# Install the Netlify CLI npm install netlify-cli -g # Login to Netlify netlify login # Link to site on Netlify netlify link
- Start Netlify Dev server
netlify dev --live - Run dev script. This script updates your bot webhook to your local Netlify Dev server.
npm run dev
- "unable to verify the first certificate": If you get this message during local development because you're working with a local web-server add
NODE_TLS_REJECT_UNAUTHORIZED=0to your.envfile. Important: only do this with local web-servers while developing. If you get this message for your production server you have a problem with your SSL certificate. More Informations. - If you get 401 errors even though your consumer key secret and everything is properly configured, there is a chance your site is not passing through the authentication headers to PHP in CGI mode. This can be fixed e.g. by adding the following line to the top of your
.htaccess:SetEnvIf Authorization "(.*)" HTTP_AUTHORIZATION=$1.