"use strict"
var userToken = process.env.AGENDOR_TOKEN;
var agendorUrl = "https://api.agendor.com.br/v1/";
var request = require('request');

module.exports.nextTask = function nextTask(sendTextMessage, sender, res) {
  request({
    url: agendorUrl + "tasks",
    headers: {"Authorization": "Token " + userToken},
    qs: {page:1, per_page:1},
    method: 'GET'
  }, function(error, response, body) {
    if (error) {
      console.log('Error sending messages: ', error)
    } else if (response.body.error) {
      console.log('Error: ', response.body.error)
    }

    var text = formatTask(body[0]);
    console.log("Text");
    console.log(text);
    console.log("End Text");
    sendTextMessage(text, sender);
    res.sendStatus(200);
  });
};

function formatTask(task) {
  var entity;
  var entityDesc;
  if (task.deal) {
    entityDesc = "o negócio";
    entity = task.deal.name;
  } else if (task.organization) {
    entityDesc = "a empresa";
    entity = task.organization.legalName;
  } else if (task.person) {
    entityDesc = "a pessoa";
    entity = task.person.name;
  }

  var text = task.text;
  var dueDate = task.dueDate;

  var textMessage = "Você possuí uma tarefa para " + entityDesc + ": " + entity + " ";
  if (dueDate) {
    textMessage += "às " + dueDate;
  }
  if (text) {
    textMessage += "\n Descrição:\n" + text;
  }
  console.log("Text Message");
  console.log(task);
  console.log("End Text Message");
  return textMessage;
}

@@ -559,17 +559,66 @@
 )%0A    %7D%0A
-%0A
+    if (!error && response.statusCode == 200) %7B%0A  
     var 
@@ -645,24 +645,26 @@
 dy%5B0%5D);%0A    
+  
 console.log(
@@ -668,24 +668,26 @@
 og(%22Text%22);%0A
+  
     console.
@@ -697,24 +697,26 @@
 (text);%0A    
+  
 console.log(
@@ -724,24 +724,26 @@
 End Text%22);%0A
+  
     sendText
@@ -769,16 +769,18 @@
 r);%0A    
+  
 res.send
@@ -792,16 +792,22 @@
 s(200);%0A
+    %7D%0A
   %7D);%0A%7D;

Fix some issues