PHP code example of shapin / talkjs

1. Go to this page and download the library: Download shapin/talkjs library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

shapin / talkjs example snippets


use Shapin\TalkJS\TalkJSClient;
use Symfony\Component\HttpClient\HttpClient;

$httpClient = HttpClient::create([
    'base_uri' => 'https://api.talkjs.com/v1/'.self::APP_ID.'/',
    'auth_bearer' => self::SECRET_KEY,
    'headers' => [
        'Content-Type' => 'application/json',
    ],
]);

$talkJSClient = new TalkJSClient($httpClient);

$filters = [
    'limit' => 50,
    'startingAfter' => 'latestMessageId'
];

// Create or update a user
$client->users()->createOrUpdate('my_custom_id', [
    'email' => '[email protected]',
]);

// Retrieve a user
$user = $client->users()->get('my_custom_id');

// Create or update a conversation
$client->conversations()->createOrUpdate('my_custom_id', [
    'subject' => 'My new conversation',
    'participants' => ['my_user_id_1', 'my_user_id_2'],
    'welcomeMessages' => ['Welcome!'],
    'custom' => ['test' => 'test'],
    'photoUrl' => null
]);

// Retrieve a conversation
$conversation = $client->conversations()->get('my_custom_id');

// Find conversations
$conversations = $client->conversations()->find($filters);

// Join a conversation
$client->conversation()->join('my_conversation_id', 'my_user_id');

// Leave a conversation
$client->conversation()->leave('my_conversation_id', 'my_user_id');

// Update participation settings
$params = [
  'notify' => true, // Boolean, default true
  'access' => 'ReadWrite' // ReadWrite or Read, default ReadWrite 
];
$client->conversations()->updateParticipation('my_conversation_id', 'my_user_id', $params);

$custom = [
  'foo' => 'bar'
];
// Find Messages
$client->conversations()->findMessages('my_conversation_id', $filters);

// Post a system message
$client->conversations()->postSystemMessage('my_conversation_id', 'message_text', $custom);

// Post a user message
$client->conversations()->postUserMessage('my_conversation_id', 'my_user_id', 'message_text', $custom);