-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
1. composer.json 多一個逗號,會無法 install,已修正 2. LINE Notify 註冊與推播已沒問題 3. 為防止一個帳號多次連動,造成多次通知與推播效能,往後可能需要做身份驗證 4. 例外處理沒寫的很好... 需要高手相救 5. 本次有新增 table,務必 migrate
- Loading branch information
1 parent
cd0c7b3
commit db8e815
Showing
11 changed files
with
11,861 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,3 +4,4 @@ Homestead.yaml | |
Homestead.json | ||
.env | ||
.idea/ | ||
.DS_Store |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
<?php | ||
|
||
namespace MOLiBot\Http\Controllers; | ||
|
||
use Illuminate\Http\Request; | ||
use MOLiBot\LINE_Notify_User; | ||
use SoapBox\Formatter\Formatter; | ||
use \GuzzleHttp\Client as GuzzleHttpClient; | ||
use \GuzzleHttp\Exception\TransferException as GuzzleHttpTransferException; | ||
|
||
|
||
class LINENotifyController extends Controller | ||
{ | ||
|
||
private $redirect_uri; | ||
private $client_id; | ||
private $client_secret; | ||
|
||
public function __construct() | ||
{ | ||
$this->redirect_uri = \Config::get('line.line_notify_redirect_uri'); | ||
$this->client_id = \Config::get('line.line_notify_client_id'); | ||
$this->client_secret = \Config::get('line.line_notify_client_secret'); | ||
} | ||
|
||
public static function sendMsg($access_token, $msg) | ||
{ | ||
$client = new GuzzleHttpClient(); | ||
try { | ||
$response = $client->request('POST', 'https://notify-api.line.me/api/notify', [ | ||
'headers' => [ | ||
'Authorization' => 'Bearer ' . $access_token, | ||
], | ||
'form_params' => [ | ||
'message' => $msg, | ||
], | ||
'timeout' => 10 | ||
]); | ||
} catch (GuzzleHttpTransferException $e) { | ||
$status = $e->getCode(); | ||
if ($status == 400) { | ||
throw new \Exception('400 - Unauthorized request'); | ||
} elseif ($status == 401) { | ||
throw new \Exception('401 - Invalid access token'); | ||
} elseif ($status == 500) { | ||
throw new \Exception('500 - Failure due to server error'); | ||
} else { | ||
throw new \Exception('Processed over time or stopped'); | ||
} | ||
} | ||
return $response; | ||
} | ||
|
||
public function getStatus($access_token) | ||
{ | ||
$client = new GuzzleHttpClient(); | ||
try { | ||
$response = $client->request('GET', 'https://notify-api.line.me/api/status', [ | ||
'headers' => [ | ||
'Authorization' => 'Bearer ' . $access_token, | ||
], | ||
'timeout' => 10 | ||
]); | ||
$response = $response->getBody()->getContents(); | ||
$formatter = Formatter::make($response, Formatter::JSON); | ||
$json = $formatter->toArray(); | ||
|
||
return $json; | ||
} catch (GuzzleHttpTransferException $e) { | ||
return $e; | ||
} | ||
|
||
} | ||
|
||
public function auth(Request $request) | ||
{ | ||
$code = $request->query('code', false); | ||
$state = $request->query('state', false); | ||
if ($code) { | ||
$client = new GuzzleHttpClient(); | ||
// get access_token | ||
try { | ||
$response = $client->request('POST', 'https://notify-bot.line.me/oauth/token', [ | ||
'headers' => [ | ||
'User-Agent' => 'MOLi Bot', | ||
'cache-control' => 'no-cache' | ||
], | ||
'form_params' => [ | ||
'grant_type' => 'authorization_code', | ||
'code' => $code, | ||
'redirect_uri' => $this->redirect_uri, | ||
'client_id' => $this->client_id, | ||
'client_secret' => $this->client_secret | ||
], | ||
'timeout' => 10 | ||
]); | ||
|
||
$response = $response->getBody()->getContents(); | ||
$formatter = Formatter::make($response, Formatter::JSON); | ||
$json = $formatter->toArray(); | ||
$access_token = $json['access_token']; | ||
$success = true; | ||
LINE_Notify_User::create([ | ||
'access_token' => $access_token | ||
]); | ||
} catch (GuzzleHttpTransferException $e) { | ||
$status = $e->getCode(); | ||
if ($status == 400) { | ||
$error = '400 - Unauthorized request'; | ||
return view('LINE/notify_auth', compact('error')); | ||
} else { | ||
$error = 'Other - Processed over time or stopped'; | ||
return view('LINE/notify_auth', compact('error')); | ||
} | ||
} | ||
|
||
// send a welcome message | ||
try { | ||
$msg = "\n歡迎使用暨大通知,此服務由 MOLi 實驗室維護\n如有疑問可至粉專或群組詢問\nhttps://moli.rocks"; | ||
$this->sendMsg($access_token, $msg); | ||
} catch (\Exception $e) { | ||
return $e->getCode(); | ||
} | ||
|
||
// get status | ||
try { | ||
$json = $this->getStatus($access_token); | ||
LINE_Notify_User::where('access_token', $access_token) | ||
->update([ | ||
'targetType' => $json['targetType'], | ||
'target' => $json['target'] | ||
]); | ||
} catch (\Exception $e) { | ||
return $e->getCode(); | ||
} | ||
|
||
return view('LINE/notify_auth', compact('success')); | ||
|
||
} else { | ||
// 歡迎畫面 | ||
$client_id = $this->client_id; | ||
$redirect_uri = $this->redirect_uri; | ||
return view('LINE/notify_auth', compact('client_id', 'redirect_uri')); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
namespace MOLiBot; | ||
|
||
use Illuminate\Database\Eloquent\Model; | ||
|
||
|
||
class LINE_Notify_User extends Model | ||
{ | ||
protected $table = 'line_notify_users'; | ||
|
||
protected $fillable = ['access_token', 'targetType', 'target', 'sid', 'email']; | ||
|
||
public static function getAllToken() | ||
{ | ||
return static::all()->pluck('access_token')->toArray(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?php | ||
|
||
return [ | ||
/* | ||
|-------------------------------------------------------------------------- | ||
| LINE Notify Client ID [REQUIRED] | ||
|-------------------------------------------------------------------------- | ||
| | ||
| Your LINE Notify's Access Client ID. | ||
| Example: adTHJiKiA39mELihBinK | ||
| | ||
| Refer for more details: | ||
| https://notify-bot.line.me/doc/ | ||
| | ||
*/ | ||
'line_notify_client_id' => env('LINE_NOTIFY_CLIENT_ID', 'YOUR-ID'), | ||
|
||
/* | ||
|-------------------------------------------------------------------------- | ||
| LINE Notify Client Secret [REQUIRED] | ||
|-------------------------------------------------------------------------- | ||
| | ||
| Your LINE Notify's Access Client ID. | ||
| Example: 3VF2nKo3RdYIUUilOPdUXIZeDfIOPsy0rgSi4 | ||
| | ||
| Refer for more details: | ||
| https://notify-bot.line.me/doc/ | ||
| | ||
*/ | ||
'line_notify_client_secret' => env('LINE_NOTIFY_CLIENT_SECRET', 'YOUR-SECRET'), | ||
|
||
/* | ||
|-------------------------------------------------------------------------- | ||
| Line Notify Redirect URI [REQUIRED] | ||
|-------------------------------------------------------------------------- | ||
| | ||
| Your LINE Notify's Authentication page URI. | ||
| The format should be "https://{YOUR_DOMAIN}/auth" | ||
| Example: https://api.moli.rocks/line-notify-auth | ||
| | ||
| Refer for more details: | ||
| https://notify-bot.line.me/ | ||
| | ||
*/ | ||
'line_notify_redirect_uri' => env('LINE_NOTIFY_REDIRECT_URI', 'https://bot.moli.rocks/line-notify-auth'), | ||
]; |
36 changes: 36 additions & 0 deletions
36
database/migrations/2019_01_03_202843_create_line_notify_users_table.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
use Illuminate\Support\Facades\Schema; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Database\Migrations\Migration; | ||
|
||
class CreateLINENotifyUsersTable extends Migration | ||
{ | ||
/** | ||
* Run the migrations. | ||
* | ||
* @return void | ||
*/ | ||
public function up() | ||
{ | ||
Schema::create('line_notify_users', function (Blueprint $table) { | ||
$table->increments('id'); | ||
$table->string('access_token'); | ||
$table->string('targetType')->nullable(); | ||
$table->string('target')->nullable(); | ||
$table->string('sid')->nullable(); // 學號,保留用來辨識 | ||
$table->string('email')->nullable(); // Email,保留用來辨識 | ||
$table->timestamps(); | ||
}); | ||
} | ||
|
||
/** | ||
* Reverse the migrations. | ||
* | ||
* @return void | ||
*/ | ||
public function down() | ||
{ | ||
Schema::dropIfExists('line_notify_users'); | ||
} | ||
} |
Oops, something went wrong.
db8e815
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
學長好猛
db8e815
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
要加哪個帳號?
db8e815
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
學長請:https://bot.moli.rocks/line-notify-auth
功能跟 Telegram 版本的一樣