|
37 | 37 | #### Conversations |
38 | 38 |
|
39 | 39 | - [Fetch conversations](#fetch-conversations) |
| 40 | +- [Fetch single conversation](#fetch-single-conversation) |
| 41 | +- [Update single conversation](#update-single-conversation) |
40 | 42 | - [Fetch messages](#fetch-messages) |
41 | 43 |
|
42 | 44 | #### Teams |
@@ -754,6 +756,117 @@ if ($response->isSuccess()) { |
754 | 756 | } |
755 | 757 | ``` |
756 | 758 |
|
| 759 | +### <a name='fetch-single-conversation'>Fetch single conversation</a> |
| 760 | + |
| 761 | +```php |
| 762 | +// parameters |
| 763 | +$params = [ |
| 764 | + // Conversation UUID, MANDATORY |
| 765 | + 'conversation_id' => '...' |
| 766 | +]; |
| 767 | + |
| 768 | +try { |
| 769 | + $response = $sendbeeApi->getConversation($params); |
| 770 | +} catch (\Sendbee\Api\Support\DataException $ex) { |
| 771 | + // handle missing data |
| 772 | + // this happens when required data was not provided |
| 773 | + echo "Missing required data. ", $ex->getMessage(); |
| 774 | +} catch (\Exception $ex) { |
| 775 | + // handle exception thrown by GuzzleHttp |
| 776 | + // this is most likely due to a network issue |
| 777 | + echo "Could not contact backend endpoint. ", $ex->getMessage(); |
| 778 | +} |
| 779 | + |
| 780 | + |
| 781 | +if ($response->isSuccess()) { |
| 782 | + // everything is OK |
| 783 | + /** |
| 784 | + * @var $conversation \Sendbee\Api\Models\Conversation |
| 785 | + */ |
| 786 | + $conversation = $response->getData(); |
| 787 | + |
| 788 | + echo "\n ID: ", $conversation->id; |
| 789 | + echo "\n folder: ", $conversation->folder; |
| 790 | + echo "\n chatbot_active: ", $conversation->chatbot_active; |
| 791 | + echo "\n platform: ", $conversation->platform; |
| 792 | + echo "\n created_at: ", $conversation->created_at; |
| 793 | + |
| 794 | + echo "\n contact -> id: ", $conversation->contact->id; |
| 795 | + echo "\n contact -> name: ", $conversation->contact->name; |
| 796 | + echo "\n contact -> phone: ", $conversation->contact->phone; |
| 797 | + |
| 798 | + echo "\n last_message -> direction: ", $conversation->last_message->direction; |
| 799 | + echo "\n last_message -> status: ", $conversation->last_message->status; |
| 800 | + echo "\n last_message -> inbound_sent_at: ", $conversation->last_message->inbound_sent_at; |
| 801 | + echo "\n last_message -> outbound_sent_at: ", $conversation->last_message->outbound_sent_at; |
| 802 | +} else { |
| 803 | + /** |
| 804 | + * @var $error \Sendbee\Api\Transport\ResponseError |
| 805 | + */ |
| 806 | + $error = $response->getError(); |
| 807 | + if ($error) { |
| 808 | + echo "\n error type: ", $error->type; |
| 809 | + echo "\n error details: ", $error->detail; |
| 810 | + } |
| 811 | +} |
| 812 | +``` |
| 813 | + |
| 814 | + |
| 815 | +#update-single-conversation |
| 816 | +### <a name='fetch-messages'>Update a single conversation</a> |
| 817 | + |
| 818 | +```php |
| 819 | +// parameters |
| 820 | +$params = [ |
| 821 | + // Conversation UUID, MANDATORY |
| 822 | + 'conversation_id' => '...', |
| 823 | + // Assigned "folder" - 'open' or 'done' |
| 824 | + 'folder' => 'open|done' |
| 825 | +]; |
| 826 | + |
| 827 | +try { |
| 828 | + $response = $sendbeeApi->updateConversation($params); |
| 829 | +} catch (\Sendbee\Api\Support\DataException $ex) { |
| 830 | + // handle missing data |
| 831 | + // this happens when required data was not provided |
| 832 | + echo "Missing required data. ", $ex->getMessage(); |
| 833 | +} catch (\Exception $ex) { |
| 834 | + // handle exception thrown by GuzzleHttp |
| 835 | + // this is most likely due to a network issue |
| 836 | + echo "Could not contact backend endpoint. ", $ex->getMessage(); |
| 837 | +} |
| 838 | + |
| 839 | + |
| 840 | +if ($response->isSuccess()) { |
| 841 | + // everything is OK |
| 842 | + $data = $response->getData(); |
| 843 | + |
| 844 | + foreach ($data as $message) { |
| 845 | + /** |
| 846 | + * @var $message \Sendbee\Api\Models\Message |
| 847 | + */ |
| 848 | + echo "\n body: ", $message->body; |
| 849 | + echo "\n media_type: ", $message->media_type; |
| 850 | + echo "\n media_url: ", $message->media_url; |
| 851 | + echo "\n status: ", $message->status; |
| 852 | + echo "\n direction: ", $message->direction; |
| 853 | + echo "\n sent_at: ", $message->sent_at; |
| 854 | + |
| 855 | + } |
| 856 | +} else { |
| 857 | + /** |
| 858 | + * @var $error \Sendbee\Api\Transport\ResponseError |
| 859 | + */ |
| 860 | + $error = $response->getError(); |
| 861 | + if ($error) { |
| 862 | + echo "\n error type: ", $error->type; |
| 863 | + echo "\n error details: ", $error->detail; |
| 864 | + } |
| 865 | +} |
| 866 | +``` |
| 867 | + |
| 868 | + |
| 869 | + |
757 | 870 | ### <a name='fetch-messages'>Fetch messages in a conversation</a> |
758 | 871 |
|
759 | 872 | ```php |
|
0 commit comments