HTTP Requests
HTTP requests are the next feature we will look at to expand your app functionality. If you want to connect your app to the outside world, the HTTP property enables users to invoke an external web service.
In this topic, we will create a slash command get
for our Hello World app. This command executes a GET
HTTP request based on the given URL. You can also use our Tester App or any app of your choice. Make sure that a Rocket.Chat server is ready to deploy the app.
Step 1: Register the slash command
The slash command must be registered in the app's main class, at the root of the project.
Here, we import our new slash command class and then register it in the app's configuration.
Step 2: Create the slash command
If you haven't created a separate directory for slash commands, it is recommended to create a
commands
directory at the root of the project.Create the
HTTPRequestCommand.ts
file in this directory.Then enter the code shown below:
This code implements the following:
The slash command is called
get
.When executed, it uses the argument that the user passed after the command as the URL.
The argument is mandatory. If no argument is provided, an error is thrown.
Perform the
GET
request using the provided argument.
Optionally, you can store the GET
request in a console constant. When the command is executed, it is logged.
Step 3: Print the request to a conversation
Now, instead of logging console output to the instance's log, let's output it to the conversation.
Add the following private method to HTTPRequestCommand.ts
.
This function implements the following:
Gets the user who invoked the command (in this case, you).
Selects the room where the command was executed.
Sets the received string as the message.
Sends the message to the room.
Then, append the following code to the end of the executor method:
Instead of simply sending the request and not capturing the response, we store the response in a constant, format its content as a string, and transmit it using our new sendMessage
method.
Note: To learn more about messaging, see the IMessageBuilder documentation.
Step 4: Deploy the app
To deploy the app, run:
The
<server_url>
parameter is the URL of your Rocket.Chat server.Replace the placeholders with the URL, username, and password for your server, respectively.
After executing this command, your application will be deployed to the server.
Packaging your app
Alternatively, you can execute the rc-apps package
command. This gives you a compressed zip file of your app that you can upload as a private app to your Rocket.Chat server.
Step 5: Test the app
After deploying the application, enter /get <some_url>
in any channel and the app will send a GET
request to the specified URL. In this example, we will use JSONPlaceholder to obtain dummy data to test our app:
Enter
/get https://jsonplaceholder.typicode.com/todos/1
in a chat.You receive the following response in the chat:
Similarly, you can apply this feature to connect with any domain of your choice.
Next, let's gain an understanding of events and the Apps-Engine-supported features to handle Rocket.Chat events.
Last updated