https://github.com/lastmjs/oc-governance-bot
https://github.com/lastmjs/oc-governance-bot
Last synced: 2 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/lastmjs/oc-governance-bot
- Owner: lastmjs
- License: mit
- Created: 2022-01-27T21:22:40.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2022-04-06T01:11:42.000Z (about 3 years ago)
- Last Synced: 2025-03-23T22:37:01.683Z (3 months ago)
- Language: Rust
- Size: 34.2 KB
- Stars: 10
- Watchers: 2
- Forks: 2
- Open Issues: 6
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# oc-governance-bot
This bot runs entirely on the Internet Computer. It listens for open NNS proposals, using the IC heartbeat functionality to constantly poll the NNS governance canister. When an open proposal is found, its information is put into a text message that is then sent to the corresponding OpenChat group for that proposal's topic.
OpenChat currently isn't open source, but the team shared enough Candid information with me that I was able to get my bot to work. If you want to create your own bot, below I show what it took to get my bot going.
1. Register the bot as a user on OpenChat
2. Create the bot's user canister on OpenChat
3. Join the bot to a group
4. Test sending a message### Register the bot as a user on OpenChat
Simply call [register_governance_bot_user](https://github.com/lastmjs/oc-governance-bot/blob/main/canisters/bot/src/bot_utilities.rs#L25) with the correct username. You will need to modify this function with your bot's username.
### Create the bot's user canister on OpenChat
Simply call [create_governance_bot_user_canister](https://github.com/lastmjs/oc-governance-bot/blob/main/canisters/bot/src/bot_utilities.rs#L38). This will instruct OpenChat to register the canister of this user.
### Join the bot to a group
Hopefully you have the bot user's canister id from the previous step. If not you can call [get_governance_bot_user_canister_id](https://github.com/lastmjs/oc-governance-bot/blob/main/canisters/bot/src/bot_utilities.rs#L49) to get it, or you can join a chat with the bot in the OpenChat UI and look at the canister id in the address bar of the browser.
Next you will call [governance_bot_join_group](https://github.com/lastmjs/oc-governance-bot/blob/main/canisters/bot/src/bot_utilities.rs#L70) and pass in the canister id of the group that you want to join. You should have created the group already. The canister id can be found in the address bar of the browser.
### Test sending a message
You can test your bot using the [send_message](https://github.com/lastmjs/oc-governance-bot/blob/main/canisters/bot/src/bot_utilities.rs#L84) function. You might need to modify it to update the message id (if you have already used `0` as a message id) and the bot's username.
## Running the bot
The key to running the bot is the [heartbeat](https://github.com/lastmjs/oc-governance-bot/blob/main/canisters/bot/src/lib.rs#L52) function. Once you push that to the IC, the `heartbeat` function will run every few seconds. You can read more about `canister_heartbeat` here: https://smartcontracts.org/docs/interface-spec/index.html#_heartbeat
The logic should be pretty easy to follow if you start at the `heartbeat` function. Basically every few seconds the NNS governance canister is polled to see if there are any open proposals. As long as the proposal id has not already been processed (stored locally in a map), then a message is sent to the appropriate group.