Make A Discord Bot That Can Upload, Pin, and Search IPFS
Welcome to the very first edition of our monthly “What you can build with Temporal in 2 hours or less” series ! This is part of our continued mission to help awareness and education of not only RTrades platform Temporal, but also IPFS and the distributed web as we migrate to the great wonders of Web 3.
You’ll have a discord bot that can:
- Upload and pin data to IPFS using warp
- Index data through IPFS search engine Lens+Temporal
- Search IPFS data through Lens+Temporal
Golang Libraries Used:
Before starting you need to:
- Register an account with discord so you can generate a token for the bot to use.
- Register an account with dev.temporal.cloud our development environment.
The code for this tutorial lives here: github.com/RTradeLtd/dpinner
Now lets get started !
To make it easy to handle configuration, dpinner comes with a simple config package that you can use to pass in configuration parameters..
The first part of this is setting up authentication for when our bot starts. To do this, you must either supply the values via environment variables, or through the use of a config file.
Then we need to authenticate with Temporal’s v2 development environment, using the
In the example, the function GetJWT is used to retrieve the JWT generated when you authenticate with Temporal. This is used internally by the
thc package, but is also expected by Warp our IPFS HTTP API endpoint.
Next up you’ll need to establish an IPFS HTTP API client by connecting to Warp our high speed IPFS HTTP API endpoint. After doing this, you then need to configure
go-ipfs-api to automatically supply the JWT with all requests. This is done using the
The rest that follows is some boilerplate code from
discordgo that allows us to connect to Discord's servers, and process any incoming messages.
One thing to note is
dg.AddHandler(messageCreate) to specify a function, in this case
messageCreate that will run anytime a new message is received. It allows us to:
The below code makes some calls to other functions
processUpload. This is used to perform any search against Lens returning at most 10 results. If more than 10 results are returned, we pick 10 at a random.
This is used to process any uploads sent as attachments, with a comment
!dpinner upload, storing the data on IPFS, and returning the hash to the user.
Congratulations You’re Done !
Now you have a discord bot that can pin and upload to IPFS, as well as search for content on Lens! Additionally, a simple help menu is displayed with the command
!dpinner pin QmNfpCMR8LN6NAnYgANnEFcFptcr2QnF46EbDWLwv2jvfQ QmdytmR4wULMd3SLo6ePF4s3WcRHWcpnJZ7bHhoj3QB13v
!dpinner upload (This must be used as the comment for any uploads/attachments to be processed)
!dpinner index QmdytmR4wULMd3SLo6ePF4s3WcRHWcpnJZ7bHhoj3QB13v
!dpinner index QmdytmR4wULMd3SLo6ePF4s3WcRHWcpnJZ7bHhoj3QB13v reindex=true
!dpinner search blockchain
!dpinner search blockchain data
We have hoped you enjoyed this months series and your new discord bot ! We would appreciate any feed back and are always looking for requests for the next Temporal build. If you have a cool idea or just some questions please join RTrade’s online community, Twitter or Telegram and website. Also dont forget to show Temporal some love on Github!
For anyone new to the platform and wants to know what the heck is Temporal anyways and why we have built it, we hope this explains things.
As well for non API users, a walk-through of our easy to us web interface, the most featured third-party IPFS service on the market.