Rick Roll Your Friends Using Appwrite, Twilio, and .NET
Aditya Oberai
Posted on June 10, 2022
Being the fun-loving guy and absolute geek that I am, I've always looked for ways to use tech to prank my friends π. I remember that first prank I did 11 years back, a little shortcut hidden behind an Internet Explorer icon that shut down my friend's Windows XP based PC and gave him a mini heart-attack π. My pranks have gotten "a little more sophisticated" since then, having graduated to rick rolling them via phone calls now! π€£
The best part is that little prank is really easy to create, and I will show you how to create this prank using 3 of my favourite technologies, Appwrite, Twilio, and .NET today! π
TL;DR: It just takes a single command to install Appwrite.
docker run -it--rm\--volume /var/run/docker.sock:/var/run/docker.sock \--volume"$(pwd)"/appwrite:/usr/src/code/appwrite:rw \--entrypoint="install"\
appwrite/appwrite:1.3.2
Once your server is up and running, head over to the Appwrite Dashboard on your serverβs public IP address ( or localhost if you installed locally ) and create a new admin user account.
In order to enable the .NET 6.0 runtime for Appwrite Cloud Functions, you need to update the .env file in the Appwrite installation folder. Enter the file and add dotnet-6.0 to the comma-separated list in the environment variable _APP_FUNCTIONS_RUNTIMES. This will make the .NET runtime available in Appwrite Functions. You can then load the updated configuration using the docker-compose up -d command.
π§βπ» The Appwrite CLI
Weβll use the Appwrite CLI during this exercise as it makes the process super simple. If you have Node.js installed, the installation command is a simple
In order to create an Appwrite Cloud Function, first we must login to the Appwrite CLI using the appwrite login command using the credentials we created when setting up the Appwrite instance.
appwrite login
? Enter your email test@test.com
? Enter your password ********
? Enter the endpoint of your Appwrite server http://localhost/v1
β Success
Next up, we need to create a new Appwrite project (or link an existing one) to work with. This can be achieved via the appwrite init project command.
appwrite init project
? How would you like to start? Create a new Appwrite project
? What would you like to name your project? Project X
β Success
You can give your project any name you'd like. As soon as you create or link a project, you should notice a new appwrite.json file in the current directory which stores all the information about your project.
Now that our CLI is all set up with our Appwrite project, let's initialize our function using the appwrite init function command.
appwrite init function
? What would you like to name your function? RickRoll
? What runtime would you like to use? .NET (dotnet-6.0)
β Success
Give your function a name (I've chosen RickRoll here for convenience) and choose the .NET 6.0 runtime. This will create a new Appwrite Function in your project and set up all the boilerplate code necessary. The function's files are available in the functions/RickRoll directory, which is where we'll be working.
If you don't feel comfortable creating Appwrite Cloud Functions with .NET, please make sure to check out our blog before moving onto the next step.
π§βπ» Creating the Rick Roll Function
After initializing the RickRoll Function, please visit the functions/RickRoll directory. Our file structure here looks as follows.
Enter src/Index.cs and replace the boilerplate with the following code.
usingSystem.Collections.Generic;usingSystem.Threading.Tasks;usingNewtonsoft.Json;usingTwilio;usingTwilio.Types;usingTwilio.Rest.Api.V2010.Account;publicasyncTask<RuntimeResponse>Main(RuntimeRequestreq,RuntimeResponseres){// Convert payload from JSON string to Dictionary and get the phone number to call varpayload=JsonConvert.DeserializeObject<Dictionary<string,string>>(req.Payload);vartoPhoneNumber=payload["phoneNumber"];// Get Twilio Account SID, Auth Token, and Phone Number from the Environment VariablesvaraccountSID=req.Env["TWILIO_ACCOUNTSID"];varauthToken=req.Env["TWILIO_AUTHTOKEN"];vartwilioPhoneNumber=req.Env["TWILIO_PHONENUMBER"];//Initialize the Twilio SDKTwilioClient.Init(accountSID,authToken);// Create the phone call with the Twilio Voice APIvarcall=CallResource.Create(to:newPhoneNumber(toPhoneNumber),from:newPhoneNumber(twilioPhoneNumber),twiml:newTwiml("<Response><Play>https://demo.twilio.com/docs/classic.mp3</Play></Response>"));// Return the response from the Twilio SDKreturnres.Json(new(){{"twilioResponse",call}});}
Letβs go over the code we have here. From the Cloud Functions documentation, we see that the payload and environment variables are available through the request object. Here, we take the payload, which we receive as a JSON string, and convert it into a Dictionary. This is achieved using the Newtonsoft.Json library. We grab the phone number we want to call from the deserialized payload as well.
We then retrieve the Twilio Account SID, Twilio Auth Token, and our Twilio Phone Number from our environment variables and use the Twilio C# / .NET SDK to make a phone call with a very special audio π.
In order to make sure that Appwrite installs the Newtonsoft.Json library and the Twilio SDK to our function, we must include them in the Function.csproj file.
Now that our function is ready, we can deploy it to our Appwrite instance using the appwrite deploy function command.
appwrite deploy function
? Which functions would you like to deploy? RickRoll (62a1a474b154de566308)
βΉ Info Deploying function RickRoll ( 62a1a474b154de566308 )
βΉ Info Ignoring files using configuration from appwrite.json
β Success Deployed RickRoll ( 62a1a474b154de566308 )
The function should be visible within your Appwrite Instance here.
One last thing we must do before we can test the function is add the necessary environment variables to the Function's Settings page.
TWILIO_ACCOUNTSID: Twilio Account SID
TWILIO_AUTHTOKEN: Twilio Auth Token
TWILIO_PHONENUMBER: Twilio Phone Number to make the call from
Now our function is fully ready to test! π₯³
π Testing the Rick Roll Cloud Function
Once the function is deployed, we can head over to the Appwrite console and execute the function with the payload in the following format.
{"phoneNumber":"+919876543210"}
Executing the function should lead to the phone number you entered receiving a call as shown.
We can also find the response from the Twilio SDK in the Function's Logs page.
And that brings us to the end folks! I sincerely hope you enjoyed yourself learning how you can leverage Appwrite Cloud Functions to build a fun little prank project. Feel free to visit the GitHub repo for this project (and give it a star π if you like it).
In case you want to learn more about Appwrite Cloud Functions, feel free to visit the Functions Guide and feel free to visit the Appwrite Discord Server if you need any help.
Thanks a lot and hope you have a great day! β€οΈ