Slack Governance Alerts Bot
Prerequisites
Before we begin, make sure you have the following:
- A Leap webhooks (opens in a new tab) account.
- A Slack (opens in a new tab) account
- A Replit (opens in a new tab) account
- Familiarity with Python
Overview
Here's a brief outline of the steps we'll cover in this guide:
- Creating a new webhook!
- Configuring Leap’s Notifications Webhook
- Writing the Python code for the bot
- Setting up the Slack bot
Let's dive in!
Step 1: Setting up the Python App on Replit
- Log into Replit and click the "Create Repl" button in your menu.
- Search and select the Flask template. Name the project whatever you like.
- Write the following code in yourÂ
main.py
 file. This script is tailored to Governance notifications.
import os
from flask import Flask, request, Response
import requests
import traceback
import sys
import json
app = Flask(__name__)
# Set up Slack Webhook url in order to send message to your slack channel
SLACK_WEBHOOK_URL = os.environ["SLACK_WEBHOOK_URL"]
def get_parsed_message(event):
event_type = event.get("__type", None)
text = None
if event_type == "cosmos.gov.deposit":
text = "Deposit for chain:{},proposalID: {} {}{}, ".format(
event["blockchain"],
event["proposalId"],
event["tokens"][0]["quantity"],
event["tokens"][0]["denomination"],
)
elif event_type == "cosmos.gov.vote":
text = "Vote:{} received for chain:{},proposalID: {}".format(
event["option"], event["blockchain"], event["proposalId"])
elif event_type == "cosmos.gov.submitProposal":
text = "Governance Proposal Submitted chain:{},proposalID: {}, {}{}".format(
event["option"],
event["blockchain"],
event["proposalId"],
event["tokens"][0]["quantity"],
event["tokens"][0]["denomination"],
)
return text
def send_slack_message(message):
# Send a message to Telegram using Telegram API
url = SLACK_WEBHOOK_URL
payload = json.dumps({"text": message})
headers = {"Content-Type": "application/json"}
response = requests.request("POST", url, headers=headers, data=payload)
print(response.text)
return
def handler(event):
# Handle the event and send a message to Telegram
print(event)
message = get_parsed_message(event)
if message is None:
print("Event is not parsed")
return
send_slack_message(message)
@app.route("/", methods=["GET"])
def main():
# Return a success response to the request
return Response(status=200)
@app.route("/", methods=["POST"])
def webhook():
# Handle incoming webhook requests
handler(request.json)
# Return a success response to the request
return Response(status=200)
# Start Flask app
if __name__ == "__main__":
app.run(host="0.0.0.0", port=81)
Step 2: Create a New Bot on Slack and Link It to the Server
- Head over to slack (opens in a new tab) and create a Bot User.
- Click on Create New App. Enter an App Name for your new Bot. Also select the workspace in which you want to use this bot.
- Click on your newly created app to navigate to App level settings.
- Open Incoming Webhooks from the sidepanel. We will be using Webhooks to send the slack message, click on Activate Incoming Webhooks to allow bot to send messages via webhooks. Click on Add New Webhooks to Workspace to start creating a webhook for your workspace.
- Inside the drop-down menu that opens, select the channel name for which you want to send the slack message to.
- You should now be able to see a Webhook URL for the channel you selected previously. Lets copy the webhook URL and update the same in our python code on replit.
- Open the Secrets tool in Replit. Store the API token in an environment variable SLACK_WEBHOOK_URL. This ensures the token remains secure and accessible to your Python script.
Step 3: Create a new webhook
The server’s URL will follow the pattern <project_name>.<user_name>.repl.co
 . Once you've entered the URL, click "Create Webhook" to activate it, and your bot will start receiving data from Alchemy whenever the specified event takes place.
Your Telegram bot is now set up and ready to send notifications for DAI transfers or any other event you've tailored the handler to process.
Turn off your Alchemy webhook when you're not using it in order to preserve your credits.
-
Log in to your Leap webhooks (opens in a new tab) account.
-
Click "New Webhook" and fill in the following details. Name of the web : Give the webhook you have created a name Select Chain from the dropdown : We have selected osmosis for this example. Refer to the docs (opens in a new tab) to get list of chains supporter *Transaction type(s) : Multi-Select from the drop down all the transactions you want to listen to. For the purpose of setting up Governance alerts you can select “Gov Deposit”, “Submit Proposal” and “Vote”. Refer to the docs (opens in a new tab) to get a list of transaction types supports
Webhook URL:* Paste the webhook url which you copied from replit, it will be of the format <project_name>.<user_name>.repl.co
- Click on “Create” once you have entered all details.
- Verification code is sent to your python server hosted on replit, just copy the verification code that is printed on the terminal in replit, and paste it in the dialog box asking for Verification key in leap notifications portal.
- Your bot is ready to receive and start sending notifications to Slack now.
Production
Your development server will stop running when you close the Replit window. To keep it live all the time, you can use Replit Deployments.
If you use a Deployment, make sure to use the Deployment URL in your Alchemy webhook instead of the development URL.