Skip to content

6. Testing And Deployment

Aditya Sharma edited this page May 7, 2024 · 3 revisions

🧪 Testing the Bot (locally):

Now that we have made index.js and start_cmd.js, and also set up wrangler.toml with correct credentials, it's time to test the bot locally for any error or bugs! We'll need wrangler CLI and ngrok (https://ngrok.io) in this step, so make sure you have everything installed!

NOTE: Wrangler CLI and Ngrok are a must! you can check the Prerequisites Page for Installation.

Moving up...

🕸️ Spinning up the local tunnel:

  • Let's startup a local tunnel using ngrok first:
  • On the cmd/shell/ps, use ngrok http http://127.0.0.1:8787/, On successful tunnel we'll get the following message:
ngrok                         
Session Status                online
Account                       xxxxxx@gmail.com (Plan: Free)
Version                       3.9.0
Region                        xxxx (in)
Latency                       42ms
Web Interface                 http://127.0.0.1:4040
Forwarding                    https://12f3-12-12-12-123.ngrok-free.app -> http://127.0.0.1:8787/

Connections                   ttl     opn     rt1     rt5     p50     p90     
                           0       0       0.00    0.00    0.00    0.00   
  • Now copy the Forwarding aka our newly made Webhook URL, which in my case is, https://12f3-12-12-12-123.ngrok-free.app
  • Now paste the URL in wrangler.toml, in the WEBHOOK_URL env variable with the endpoint as /bot. That would be https://12f3-12-12-12-123.ngrok-free.app/bot and save the file.

✈ Starting up the local development server:

  • We will use wrangler dev command to spin up the local dev server on 127.0.0.1:8787
  • Upon using wrangler dev we will see the following message on our console as a successful local run!
⛅️ wrangler 3.30.1 (update available 3.53.1)
-------------------------------------------------------
▲ [WARNING] The entrypoint src\index.js ....

Your worker has access to the following bindings:
- Vars:
- REPO_VERSION: "1.0.0"
- BOT_TOKEN: "6292xxxxxxxx:AAH-cxKixvJOnzxxxxxxx..."
- WEBHOOK_URL: "https://12f3-12-12-12-123.ngrok-fr..."
- SECRET_TOKEN: "npnsqBxxxxxxxxxxxxxxxxxxDR0sRgR..."
- DROP_PENDING_UPDATES: "True"
⎔ Starting local server...
[wrangler:inf] Ready on http://127.0.0.1:8787

We can ignore the warnings above.


🔧 Setting up the Webhook:

Now that we have our local tunnel and local dev server up and running! It's time to set the Webhook URL at the telegram. For this just send a POST request through Postman or thunder client (vs code) to the URL https://12f3-12-12-12-123.ngrok-free.app/set-webhook. And the server would return,

Webhook was set

After everything's Done! time to message our brand new bot!

image

Seems to be working fine! 😄


☁️ Deployment / Deploy to Workers:

We can now close the ngrok tunnel and the wrangler dev server, let's start deploying our bot on cloudflare workers! 😄

⚠️ Make sure you have a Cloudflare account before proceeding further.

🖥️ Using wrangler CLI to deploy:

  1. Logging in with wrangler CLI:

     wrangler login
    

    Follow the login steps to login into your Cloudflare account.

  2. Rename the name var in wrangler.toml at line 1 to your preferred worker's name. Here I renamed it to samplebot

  3. Deploy / Publish the worker:

     wrangler deploy
    
    • Here is the message after deployment was successful:
      Your worker has access to the following bindings:
      - Vars:
        - REPO_VERSION: "1.0.0"
        - BOT_TOKEN: "629xxxxxxx:AAH-c9Kxxxxxxxxxxxx..."
        - WEBHOOK_URL: "https://28f0xx-xx-xxx-xxx.ngrok-free...."
        - SECRET_TOKEN: "npnsqxxxxxxxxxxxxxxxxxxxRgR..."
        - DROP_PENDING_UPDATES: "True"
      Total Upload: 15.13 KiB / gzip: 2.65 KiB
      Uploaded samplebot (2.74 sec)
      Published samplebot (8.30 sec)
        https://samplebot.subdomain.workers.dev
      Current Deployment ID: 99xxxxxx0a-41xxx4-4xxd-axxd-92xxxxx60
  4. Updating the WEBHOOK_URL through Cloudflare Dashboard:

    • Goto your CF (Cloudflare) Dashboard and go to Workers & Pages / Overview / samplebot.
    • Now open your worker and goto the Settings tab, and click on the Triggers tab, and under Routes category you'll see your workers address, copy that.
    • Now, on the Settings tab, and click on the Variables tab to access the ENV Variables.
    • Click on Edit Variables, and replace the WEBHOOK_URL with the workers address that you just copied, with the endpoint as /bot, that'd be https://samplebot.yoursubdomain.workers.dev/bot

    ⚠️ The /bot endpoint is necessary on the WEBHOOK_URL var.

    • Now again, send a POST request to the /set-webhook endpoint of the workers address, that'd be https://samplebot.yoursubdomain.workers.dev/set-webhook

AND ....... 👍🏻 All Done!!!

You've successfully deployed your first serverless bot on Cloudflare workers written with flaregram!

Have a nice day ahead! 😄

Clone this wiki locally