Send ZenQuotes to Discord in Windows using curl and PowerShell

Looking to inspire your Discord server with content from ZenQuotes.io? In this tutorial, we’ll show you how to use Windows PowerShell and the command-line tool curl to retrieve a random entry from the ZenQuotes API and send it to your Discord server using a webhook.

With just a few lines of code, you can automate the process of sending daily quotes to your server, adding a touch of inspiration to your community. Follow along with the step-by-step instructions below to get started.

1. Create Discord Webhook

Create a webhook in your Discord server by first navigating to the desired channel. Click on the edit channel gear icon, then navigate to Integrations. From the integration settings, click on View Webhooks.

You can create a new webhook or select from an existing entry if it exists, then click on the Copy Webhook URL button. The name and icon you choose for the Webhook will appear in the channel when posting messages.

If you get stuck, check out the official Discord documentation for more information on how to create a webhook: https://support.discord.com/hc/en-us/articles/228383668-Intro-to-Webhooks

2. Use PowerShell to send a random quote to Discord

Use the following PowerShell command to send the random quote to Discord. Replace YOUR_WEBHOOK_URL_HERE with the actual webhook URL for your Discord server.

# Get the random entry from ZenQuotes.io
$quote = (Invoke-RestMethod -Uri "https://zenquotes.io/api/random")[0]

# Define the webhook URL
$url = "YOUR_WEBHOOK_URL_HERE"

# Define the message payload
$payload = @{
    content = "$($quote.q) - $($quote.a)"
} | ConvertTo-Json

# Define the headers
$headers = @{
    'Content-Type' = 'application/json'
}

# Send the message to the Discord server using Invoke-WebRequest
Invoke-WebRequest -Uri $url -Method POST -Headers $headers -Body $payload

You can save this PowerShell script as a .ps1 file and run it using the PowerShell console or the PowerShell ISE.

Run as Batch File

If you want to run the script from a batch file, you can use the powershell.exe command to execute the PowerShell script. Here’s an example:

@echo off

powershell.exe -ExecutionPolicy Bypass -File "C:\Path\To\Your\Script.ps1"

pause

Save this file as a .bat file and simply double-click the file to run the bot. The pause command will keep the window open so that you can view any errors that may be returned.

Run as a Scheduled Task

You can use Windows Task Scheduler to run your batch file automatically based on your required criteria. You may want to modify the .ps1 file to close the cmd window after 5 seconds:

  1. Remove “pause” from batch file.
  2. Add the following to your .ps1 file to add the delay:
# REM Pause the script execution for 5 seconds
Start-Sleep -Seconds 5

# REM Exit the PowerShell console window
exit

This part is completely optional if you want to monitor the bot for any errors, otherwise you can just run the batch file as a scheduled task based on your criteria. Check out this detailed guide for more information on Windows Scheduled Tasks.

Subscribe