Examples
The examples below show how to get started using our API to accomplish some common tasks, like fetching a list of existing chats.
These examples should serve as a starting point for you to develop your solution and are not intended to be used as production code.
Fetch Chats
This example shows how to incrementally fetch all chats. Note that you should
save the after
cursor before exiting and read it when starting the next
execution so your application only fetches new chats (not yet processed).
const axios = require('axios');
const fetchSomeChats = (first, after) => (
axios({
url: 'https://api.guuru.com/graphql',
method: 'post',
headers: {
Authorization: 'Bearer <token>',
},
data: {
query: `
{
partner {
chats(
first: ${first}
${after ? `after: "${after}"` : ''}
orderBy: {
field: id
direction: asc
}
) {
edges {
node {
status
question
rating
user {
name
email
}
}
}
pageInfo {
hasNextPage
endCursor
}
}
}
}
`,
},
}).then(resp => resp.data)
);
const fetchChats = async (chatHandler) => {
const first = 100;
let hasNextPage;
// TODO: read after from persistent storage
let after;
do {
console.log(`Requesting ${first} chats after ${after}`);
const { data } = await fetchSomeChats(first, after);
const { chats } = data.partner;
chats.edges.forEach(({ node: chat }) => chatHandler(chat));
after = chats.pageInfo.endCursor;
hasNextPage = chats.pageInfo.hasNextPage;
} while (hasNextPage);
// TODO: write after to persistent storage
}
const printChat = (chat) => {
console.log('Question:', chat.question);
console.log('Status:', chat.status);
console.log('Rating:', chat.rating);
console.log('User email:', chat.user && chat.user.email);
};
fetchChats(printChat).then(() => console.log('Done'));
Fetch Payouts
This example shows how to incrementally fetch all payouts for January 2019
(1546300800000
). It will fetch 100 at a time, until all have been fetched.
Each page returns edges
, which is a list of node
elements, each
corresponding to an expert with a balance greater than zero.
Read more about Pagination and Payouts.
Amount in an integer and denotes a value in cents, period is a timestamp representing milliseconds since the Unix epoch.
<?php
function request($query, $variables) {
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_URL => 'https://api.guuru.com/graphql',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_FAILONERROR => false,
CURLOPT_HTTPHEADER => array(
'Content-Type: application/json;charset=utf-8',
'Authorization: Bearer <token>'
)
));
$json = json_encode(['query' => $query, 'variables' => $variables]);
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
$response = json_decode(curl_exec($ch));
curl_close($ch);
if ($response->errors) {
throw new Exception(json_encode($response->errors));
}
return $response;
}
function getSomePayouts($period, $first, $after) {
$query =<<<'GQL'
query Payouts($first: Int!, $after: String, $period: Timestamp!) {
partner {
payouts(period: $period) {
experts(first: $first, after: $after) {
edges {
node {
name
email
balances {
amount
currency
}
partners {
edges {
verificationCode
}
}
}
}
pageInfo {
hasNextPage
endCursor
}
}
}
}
}
GQL;
$variables = [period => $period, first => $first, after => $after];
$response = request($query, $variables);
return $response->data->partner->payouts->experts;
}
function getAllPayouts($period) {
$after;
$first = 100;
$page = [pageInfo => [endCursor => null]];
do {
$page = getSomePayouts($period, $first, $page->pageInfo->endCursor);
foreach ($page->edges as $edge) {
$expert = $edge->node;
print_r($expert);
}
} while ($page->pageInfo->hasNextPage);
}
// January 2019
getAllPayouts(1546300800000);
Create Persona
This example shows on how to create a persona on GUURU platform. The returned id must be used to handle some actions as a third party agent, such as accepting a chat.
The
providerId
is the unique id of the agent in your system of record. If multiple calls tocreatePersona
are issued with the sameproviderId
only one persona will be created. The subsequent requests will return the same id. You can also call this mutation to update the name, email or photo of an existing persona by passing the new information and the currentproviderId
.
const axios = require('axios');
const createPersona = (providerId, name, email, photoUrl) => (
axios({
url: 'https://api.guuru.com/graphql',
method: 'post',
headers: {
Authorization: 'Bearer <token>',
},
data: {
query: `
mutation CreatePersona(
$providerId: ID!
$name: String!
$email: String
$photoUrl: String
) {
createPersona(input: {
providerId: $providerId
name: $name
email: $email
photoUrl: $photoUrl
})
}
`,
variables: {
providerId,
name,
email,
photoUrl
}
},
}).then(resp => resp.data)
);
createPersona(
'some-id',
'Peter',
'peter@example.com',
'https://cdn.guuru.com/assets/logo/robotAvatar.png',
).then(({ data: response }) => {
console.log('The persona id is', response.createPersona);
});
Accept Chat
This example shows how to accept a chat.
Check Create Persona on how to create a persona to represent
an external agent and get the corresponding personaId
.
An error will be returned if the chat was already accepted. You should expect and handle this error gracefully.
const axios = require('axios');
const acceptChat = (chatId, personaId) => (
axios({
url: 'https://api.guuru.com/graphql',
method: 'post',
headers: {
Authorization: 'Bearer <token>',
},
data: {
query: `
mutation AcceptChat($id: ID!, $personaId: ID!) {
acceptChat(id: $id, input: { personaId: $personaId }) {
id
status
}
}
`,
variables: {
id: chatId,
personaId,
}
},
}).then(resp => resp.data)
);
const chatId = 'O2bqy2CsAaS7RBXZrhMOB';
const personaId = 'ab22a12e-cefd-4f67-945b-c215c42202b4';
acceptChat(chatId, personaId).then(({ data: response }) => {
console.log('The chat status is now', response.acceptChat.status);
});
Send chat message
This example shows how to send a message to a chat.
const axios = require('axios');
const sendChatMessage = (message) => (
axios({
url: 'https://api.guuru.com/graphql',
method: 'post',
headers: {
Authorization: 'Bearer <token>',
},
data: {
query: `
mutation SendChatMessage($input: ChatMessageInput!) {
sendChatMessage(input: $input)
}
`,
variables: {
input: message
},
},
}).then(resp => resp.data)
);
const chatId = "O2bqy2CsAaS7RBXZrhMOB";
const text = {
chatId,
posting: {
type: 'text',
text: 'How can I help you ?',
}
};
const attachment = {
chatId,
posting: {
type: 'attachment',
mimetype: 'image/png',
address: 'https://scontentd.guuru.com/partner/guuru-qa/attachments/OwWMbHm_2y7NJNGUTpEyb/499663b1-5528-472b-a98c-3ca540eb3186/shuttle.png',
filename: 'rocket.png',
}
};
sendChatMessage(text) // or sendChatMessage(attachment)
.then(({ data: response }) => {
console.log('Message sent?', response.sendChatMessage);
});