|
@@ -59,42 +59,80 @@ class Helper {
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- /**
|
|
|
|
|
- * Read the webhook URL / key from a media type parameters list.
|
|
|
|
|
- */
|
|
|
|
|
- public static function webhookUrl(array $mediatype): string {
|
|
|
|
|
- foreach (($mediatype['parameters'] ?? []) as $p) {
|
|
|
|
|
- $name = strtolower($p['name']);
|
|
|
|
|
- if (in_array($name, ['webhook_url', 'url', 'webhook'], true)) {
|
|
|
|
|
- return (string) $p['value'];
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ // Fixed WeCom group-robot endpoint; the per-bot secret lives in {$WECOM_KEY}.
|
|
|
|
|
+ public const WECOM_BASE = 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=';
|
|
|
|
|
+ public const KEY_MACRO = '{$WECOM_KEY}';
|
|
|
|
|
+
|
|
|
|
|
+ /** Read a global macro value; null when missing. */
|
|
|
|
|
+ public static function getGlobalMacro(string $name): ?string {
|
|
|
|
|
+ try {
|
|
|
|
|
+ $res = API::UserMacro()->get([
|
|
|
|
|
+ 'globalmacro' => true,
|
|
|
|
|
+ 'output' => ['globalmacroid', 'macro', 'value'],
|
|
|
|
|
+ 'filter' => ['macro' => $name]
|
|
|
|
|
+ ]);
|
|
|
|
|
+ return ($res && isset($res[0])) ? (string) $res[0]['value'] : null;
|
|
|
|
|
+ }
|
|
|
|
|
+ catch (\Throwable $e) {
|
|
|
|
|
+ return null;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /** Create or update a global macro. */
|
|
|
|
|
+ public static function setGlobalMacro(string $name, string $value): void {
|
|
|
|
|
+ $res = API::UserMacro()->get([
|
|
|
|
|
+ 'globalmacro' => true,
|
|
|
|
|
+ 'output' => ['globalmacroid', 'macro'],
|
|
|
|
|
+ 'filter' => ['macro' => $name]
|
|
|
|
|
+ ]);
|
|
|
|
|
+ if ($res && isset($res[0])) {
|
|
|
|
|
+ API::UserMacro()->updateGlobal([
|
|
|
|
|
+ ['globalmacroid' => $res[0]['globalmacroid'], 'value' => $value]
|
|
|
|
|
+ ]);
|
|
|
|
|
+ }
|
|
|
|
|
+ else {
|
|
|
|
|
+ API::UserMacro()->createGlobal([
|
|
|
|
|
+ ['macro' => $name, 'value' => $value]
|
|
|
|
|
+ ]);
|
|
|
}
|
|
}
|
|
|
- return '';
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
- * Update (or add) the webhook_url parameter of a media type, preserving others.
|
|
|
|
|
|
|
+ * Current robot key: {$WECOM_KEY} global macro, falling back to any legacy
|
|
|
|
|
+ * value still stored in the media type parameters.
|
|
|
*/
|
|
*/
|
|
|
- public static function setWebhookUrl(string $mediatypeid, string $url): void {
|
|
|
|
|
- $mt = self::getMediatype($mediatypeid);
|
|
|
|
|
- if (!$mt) {
|
|
|
|
|
- throw new Exception('Media type not found: '.$mediatypeid);
|
|
|
|
|
|
|
+ public static function webhookKey(?array $mediatype = null): string {
|
|
|
|
|
+ $v = self::getGlobalMacro(self::KEY_MACRO);
|
|
|
|
|
+ if ($v !== null && $v !== '') {
|
|
|
|
|
+ return $v;
|
|
|
}
|
|
}
|
|
|
- $params = [];
|
|
|
|
|
- $found = false;
|
|
|
|
|
- foreach (($mt['parameters'] ?? []) as $p) {
|
|
|
|
|
- if (in_array(strtolower($p['name']), ['webhook_url', 'url', 'webhook'], true)) {
|
|
|
|
|
- $params[] = ['name' => $p['name'], 'value' => $url];
|
|
|
|
|
- $found = true;
|
|
|
|
|
|
|
+ foreach (($mediatype['parameters'] ?? []) as $p) {
|
|
|
|
|
+ $n = strtolower($p['name']);
|
|
|
|
|
+ $val = (string) $p['value'];
|
|
|
|
|
+ if ($n === 'key' && strpos($val, '{') === false) {
|
|
|
|
|
+ return $val;
|
|
|
}
|
|
}
|
|
|
- else {
|
|
|
|
|
- $params[] = ['name' => $p['name'], 'value' => $p['value']];
|
|
|
|
|
|
|
+ if (in_array($n, ['webhook_url', 'url', 'webhook'], true)) {
|
|
|
|
|
+ if (strpos($val, 'key=') !== false) {
|
|
|
|
|
+ return substr($val, strpos($val, 'key=') + 4);
|
|
|
|
|
+ }
|
|
|
|
|
+ if (strpos($val, 'http') !== 0) {
|
|
|
|
|
+ return $val;
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
- if (!$found) {
|
|
|
|
|
- $params[] = ['name' => 'webhook_url', 'value' => $url];
|
|
|
|
|
- }
|
|
|
|
|
- API::Mediatype()->update(['mediatypeid' => $mediatypeid, 'parameters' => $params]);
|
|
|
|
|
|
|
+ return '';
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /** Persist the robot key into the {$WECOM_KEY} global macro (managed here, not in the media type). */
|
|
|
|
|
+ public static function setWebhookKey(string $key): void {
|
|
|
|
|
+ self::setGlobalMacro(self::KEY_MACRO, $key);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /** Full webhook URL for this bot = fixed base + {$WECOM_KEY}. */
|
|
|
|
|
+ public static function webhookUrl(?array $mediatype = null): string {
|
|
|
|
|
+ $key = self::webhookKey($mediatype);
|
|
|
|
|
+ return $key === '' ? '' : self::WECOM_BASE.$key;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/* ------------------------------------------------------------------ */
|
|
/* ------------------------------------------------------------------ */
|