PushCustomersToIntercom.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. use App\Models\User;
  5. use Exception;
  6. class PushCustomersToIntercom extends Command
  7. {
  8. /**
  9. * The name and signature of the console command.
  10. *
  11. * @var string
  12. */
  13. protected $signature = 'customers:pushToIntercom';
  14. /**
  15. * The console command description.
  16. *
  17. * @var string
  18. */
  19. protected $description = 'Push customers to Intercom';
  20. /**
  21. * Create a new command instance.
  22. *
  23. * @return void
  24. */
  25. public function __construct()
  26. {
  27. parent::__construct();
  28. }
  29. private function postToInterCom($url, $params){
  30. try {
  31. $postData = json_encode($params);
  32. $postHeader = [
  33. 'Authorization:Bearer ' . config('app.intercomApiSecret'),
  34. 'Accept: application/json',
  35. 'Intercom-Version: 2.3',
  36. 'content-type: application/json'
  37. ];
  38. $curl = curl_init($url);
  39. curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
  40. curl_setopt($curl, CURLOPT_POSTFIELDS, $postData);
  41. curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
  42. curl_setopt($curl, CURLOPT_POST, 1);
  43. curl_setopt($curl, CURLOPT_HTTPHEADER, $postHeader);
  44. $content = curl_exec($curl);
  45. curl_close($curl);
  46. $content = json_decode($content, true);
  47. if(isset($content['errors']) && count($content['errors'])){
  48. return ['success' => false, 'message' => $content['errors']];
  49. }
  50. return ['success' => true, 'data' => $content];
  51. } catch (Exception $e) {
  52. return ['success' => false, 'message' => $e->getMessage()];
  53. }
  54. }
  55. /**
  56. * Execute the console command.
  57. *
  58. * @return int
  59. */
  60. public function handle()
  61. {
  62. $customers = User::all();
  63. if ($this->confirm('Do you wish to push '. $customers->count() . ' customers?', true)) {
  64. foreach($customers as $customer){
  65. $phone = preg_replace('/\D+/', '', $customer->phone_number ?? $customer->cell_number);
  66. $params = [
  67. 'name' => $customer->getName(),
  68. 'email' => $customer->getEmail(),
  69. 'signed_up_at' => strtotime($customer->created_at),
  70. 'type' => 'user',
  71. ];
  72. if(strlen($phone)){
  73. $params['phone'] = '+' . $phone;
  74. }
  75. if($customer->mailing_address_line1){
  76. $params['location'] = [
  77. 'type' => 'location',
  78. 'country' => $customer->mailing_address_country,
  79. 'region' => $customer->mailing_address_state . ', ' . $customer->mailing_address_line1 . ', ' . $customer->mailing_address_line2,
  80. 'city' => $customer->mailing_address_city
  81. ];
  82. }
  83. $response = $this->postToInterCom('https://api.intercom.io/contacts', $params);
  84. if($response['success']){
  85. $this->info('Added ID: ' . $customer->id . ' | ' . $customer->displayName() );
  86. }else{
  87. $this->error('Failed ID: ' . $customer->id . ' | ' . $customer->displayName() . ' | ERROR: ' . @$response['message'][0]['code'] . ' | ' . @$response['message'][0]['message']);
  88. }
  89. }
  90. $this->info('Completed!');
  91. }else{
  92. $this->error('Aborted');
  93. }
  94. }
  95. }