Send CloudwaysBot Notifications to Asana using Cloudways API

Bots are ideal for automating communication with customers and end users, mainly because of real time streaming of alerts and messages. Cloudways offers CloudwaysBot, a very smart assistant for servers and applications hosted on Cloudways Platform. The bot provides real time notifications that help users take actions for further optimizing servers and applications.

In this article I will demonstrate how to integrate CloudwaysBot and the popular task management application, Asana. Once you are done, you will be able to send CloudwaysBot notifications to Asana as a task.

Before starting, I assume that you already have an account on Cloudways. If you don’t, sign up for an account and launch your server. Next, launch your PHP application as shown in the GIF below.

Add the Application in Asana and Get Access Token

The next step of the process is to setup the application in Asana. This will generate an access token that would be used in the later cases.

Click Create New Personal Access Token. Enter a short description of the application and then click the Create button to get the access token.

Note: Save this token carefully as Asana does not display the token once created.

Add Asana PHP Wrapper Class

To use Asana API, I will use the Asana PHP wrapper class available at GitHub. This simple class was created by Adria Jimenez.

To install this class, run the following command in Composer

Shell

1

composer require ajimix/asana-api-php-class

The Asana API class is now ready for use. Asana has a structural approach to task management with a hierarchy of workspaces, projects, and finally, tasks. I will fetch the workspace and projects from Asana.

Add require ‘vendor/autoload.php’; in index .php and initialize Asana by providing the access token. Validate it for the success response code.

index.php

PHP

1

2

3

4

5

6

7

8

9

10

11

12

$asana=newAsana(array(

'personalAccessToken'=>'Your access token here'

));

if($asana->hasError()){

echo'Error while trying to connect to Asana, response code: '.$asana->responseCode;

return;

}

Get all workspaces through this line:

index.php

PHP

1

$asana->getWorkspaces();

Now using foreach() loop, I will print all workspaces and the included projects,

The following is the result of this code. The image shows that I have two workspaces and one project under Cloudways.com workspace.

I will use the workspace ID and the project ID to create a new task in the project.

Send CloudwaysBot Alerts as Tasks to Asana

To send CloudwaysBot alerts as task in Asana, I will setup a webhook in CloudwaysBot. But before that, I will add the code to get the response of the webhook.

index.php

PHP

1

2

3

4

5

$json=file_get_contents('php://input');//get the webhook response

$actions=json_decode($json);//decode the task object

$text=$actions->text;//save the task in $text

This is a simple code snippet that will get the CloudwaysBot response and save it in $text.

Let’s create a task code and send this alert to the Asana Project. I will save project ID and workspace ID in variables.

index.php

PHP

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

$workspace='211386399674884';

$projectId='211814602016238';

// create the task

$result=$asana->createTask(array(

'workspace'=>'211386399674884',// Workspace ID

'name'=>$text,// Alert

'assignee'=>'shahroze.nawaz@cloudways.com'// Assign task to...

));

//check 201 response code for success

if($asana->hasError()){

echo'Error while trying to connect to Asana, response code: '.$asana->responseCode;

return;

}

$taskId=$asana->getData()->id;// Here we have the id of the task that have been created

// Now we do another request to add the task to a project

$asana->addProjectToTask($taskId,$projectId);

if($asana->hasError()){

echo'Error while assigning project to task: '.$asana->responseCode;

}else{

echo'Success to add the task to a project.';

}

I have created a task and get the ID of the task via $taskId = $asana->getData()->id; Next i have passed the $taskId and $projectId to assign the task to my particular project. Save the file now.

Setup the Webhook in CloudwaysBot

CloudwaysBot supports custom APIs and I will now create a webhook for the index.php file. I will use the the staging URL that is located in the Application Access Details tab.

Go to CloudwaysBot from the top navigation menu, click on the channels tab and then click the Add Channel button.

Select the API option from the drop down list.

Enter the complete URL of the index.php, set the Name of the channel and the status to Active.

Finally select the server and application alerts that you wish to send to Asana.

Now comes the exciting part. Click on the Save and Test channel button. The CloudwaysBot will send a test notification to the Asana project. If the notification goes through, the connection has been established.

Conclusion

In this article, I described how you could easily connect CloudwaysBot with Asana. This greatly streamlined project management tasks as all notifications generated by the CloudwaysBot are added as tasks to the Asana project. If you would like to contribute to the discussion or have a question, Leave a comment below!

Start Creating Web Apps on Managed Cloud Servers Now!

About Shahroze Nawaz

Shahroze is a PHP Community Manager at Cloudways – A Managed PHP Hosting Platform. He’s always in search of new frameworks and methods to implement them. Besides his coding life, he loves movies and playing soccer with friends. You can email him at shahroze.nawaz@cloudways.com