我使用 ManyChat 創建了一個 Whatsapp 聊天機器人,它要求用戶輸入他的姓名、電子郵件和電話號碼,在用戶完成向機器人提供他的資訊后,用戶輸入的內容將被保存到谷歌表格中,然后聊天機器人將發送用戶鏈接到我的網站以完成他的請求。此鏈接會將他帶到與 whatsapp 中欄位相同的表單(姓名、電子郵件和電話號碼)。
當用戶單擊鏈接并轉到我網站中的表單時,我希望表單預先填充用戶的唯一資料。我已經將我的網站與 Google Sheets API 集成在一起。我只想知道它是否可能以及如何做?
我想嘗試使用令牌 ID 或身份驗證 ID 進行操作,但我不知道如何獲取它
如果你覺得它有幫助,這里是代碼
谷歌表格服務
<?php
namespace App\Http\Services;
use Google\Client;
use Google\Service\Sheets;
use Google\Service\Sheets\Sheet;
use Google\Service\Sheets\ValueRange;
class GoogleSheetServices{
public $client, $service, $documentId, $range;
public function __construct()
{
$this->client= $this->getClient();
$this->service= new Sheets($this->client);
$this->documentId= '*my document id here*';
$this->range='A:Z';
}
public function getClient(){
$client=new Client();
$client->setApplicationName('Google Sheet Demo');
$client->setRedirectUri('http://localhost::8000/googlesheet');
$client->setScopes(Sheets::SPREADSHEETS);
$client->setAuthConfig('credentials.json');
$client->setAccessType('offline');
return $client;
}
public function readSheet(){
$doc= $this->service->spreadsheets_values->get($this->documentId,$this->range);
return $doc;
}
}
控制器
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Google\Service\Sheets;
use App\Http\Services\GoogleSheetServices;
class GoogleSheetController extends Controller
{
//
public function sheetOperation(Request $request){
$data=(new GoogleSheetServices ())->readSheet();
return view('googlesheet',compact('data'));
}
}
模型
<?php
namespace App\Models;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens;
class User extends Authenticatable
{
use HasApiTokens, HasFactory, Notifiable;
/**
* The attributes that are mass assignable.
*
* @var array<int, string>
*/
protected $fillable = [
'name',
'email',
'password',
];
/**
* The attributes that should be hidden for serialization.
*
* @var array<int, string>
*/
protected $hidden = [
'password',
'remember_token',
];
/**
* The attributes that should be cast.
*
* @var array<string, string>
*/
protected $casts = [
'email_verified_at' => 'datetime',
];
}
移民
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateGoogleOAuthsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('google_o_auths', function (Blueprint $table) {
$table->id();
$table->string('provider');
$table->string('provider_value');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('google_o_auths');
}
}
看法
<!DOCTYPE html>
<html>
<head>
<title>Contact form</title>
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.4.1/css/all.css"
integrity="sha384-5sAR7xN1Nv6T6 dT2mhtzEpVJvfS3NScPQTrOxhwjIuvcA67KV2R5Jz6kr4abQsz" crossorigin="anonymous">
<link href="https://fonts.googleapis.com/css?family=Roboto:300,400,500,700" rel="stylesheet">
</head>
<body>
<div class="main-block">
<div class="left-part">
<i class="fas fa-envelope"></i>
<i class="fas fa-at"></i>
<i class="fas fa-mail-bulk"></i>
</div>
@php
foreach($data as $value){
}
@endphp
<form action="/">
<h1>Request for Multiple Qoutes</h1>
<div class="info">
<select name="select_category" id="select_categor">
<option value="">Select Category</option>
<option value=>{{$value['0']}}</option>
</select>
<input class="fname" type="text" name="name" value={{$value['2']}} placeholder="Subject">
<input type="text" name="name" placeholder="Email" value={{$value['3']}}>
<input type="text" name="name" placeholder="Phone number" value={{$value['4']}}>
</div>
<p>Message</p>
<div>
<textarea rows="4">{{$value['1']}}</textarea>
</div>
<button type="submit" href="/">Submit</button>
</form>
</div>
</body>
</html>
這是結果 $sheetData = dd(new GoogleSheetServices())->readSheet();

uj5u.com熱心網友回復:
是的,您可以通過多種方式使用資料填充視圖,并且您使用控制器是常見的一種,我只會在您的控制器中推薦一些組織,如下所示:
$data = [];
$sheetData = (new GoogleSheetServices())->readSheet();
foreach ($sheetData as $value) {
// Option 1: as Object
$whatsAppUser = new \stdClass();
$whatsAppUser->message = $value['1'];
$whatsAppUser->name = $value['2'];
$whatsAppUser->email = $value['3'];
$whatsAppUser->phoneNumber = $value['4'];
$data[] = $whatsAppUser;
// Option 1: as Array
$data[] = [
'message' => $value['1'],
'name' => $value['2'],
'email' => $value['3'],
'phoneNumber' => $value['4'],
];
}
return view('googlesheet', compact('data'));
如果您遇到問題,請使用以下結果更新您的答案 dd((new GoogleSheetServices())->readSheet());
編輯:根據您的評論,在您的googlesheet.blade檔案中:
@foreach($data as $whatsAppUser)
<!-- if you choose option 1: -->
{{ $whatsAppUser->message }}
{{ $whatsAppUser->name }}
<!-- if you choose option 2: -->
{{ $whatsAppUser['message'] }}
{{ $whatsAppUser['name'] }}
@endforeach
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/396720.html
