Video Conferencing with Zoom插件是一款管理Zoom直播的插件,可以在WordPress中创建会议并同步到zoom账号里,也可以将zoom里现有的视频同步到WordPress。本文主要介绍一种通过REST API来自动创建zoom会议视频的方法,可以配合python或postman等工具实现批量创建zoom meetings。
目录
配置Video Conferencing with Zoom插件
安装插件后,根据插件文档将插件连接到zoom账号上,简单的说要在zoom app里创建两个app:
- Server-to-Server OAuth – 负责验证,从这里获取Account ID、Client ID和Client Secret。
- Meeting SDK – 获取ClientID和Client Secret
保存设置,如果连接不成功界面会报错。手动创建一个视频测试功能是否能正常工作。
接下来到Zoom Events > Zoom Users下找到用户列表,把主持人的User ID记录下来,后面要用到。接下来时代码部分。
代码框架
要使用REST API创建视频,就要添加Custom Rest Endpoints,所以代码从扩展REST的功能开始,具体如下所示:
class Zoom_Custom_REST_Endpoints extends WP_REST_Controller {
public function register_routes() {
$version = '1';
$namespace = 'zoom/v' . $version;
$base = 'meetings/';
register_rest_route($namespace, '/' . $base, array(
array(
'methods' => WP_REST_Server::CREATABLE,
'callback' => array($this, 'create_zoom_meeting_callback'),
'permission_callback' => array($this, 'create_zoom_meeting_permissions_check'),
'args' => $this->get_endpoint_args_for_item_schema(true),
),
));
}
public function create_zoom_meeting_callback($request) {
//这里写创建zoom meeting的主要逻辑
}
public function create_zoom_meeting_permissions_check($request) {
return current_user_can('manage_options');
}
}
add_action('rest_api_init', function () {
$custom_zoom_rest = new Zoom_Custom_REST_Endpoints();
$custom_zoom_rest->register_routes();
});
注册的custom endpoint是wp-json/zoom/v1/meeting
s,向这个地址提交post请求就可以自动创建zoom会议。
permission_callback
参数的callback负责定义访问这个endpoint的权限,这里要求具有manage_options
的权限,通常是管理员。
create_zoom_meeting_callback()
函数是最重要的部分,负责创建zoom meeting。
创建zoom会议的代码
主要有两个步骤:
- 创建一个wordpress custom post type,也就是slug为zoom-meetings的CPT,并返回post ID。
- 使用Zoom REST API在我们的账户里创建zoom视频,并将信息写进post meta里,与对应的post产生关联。
public function create_zoom_meeting_callback($request) {
$title = $request->get_param('title');
$content = $request->get_param('content');
$args = array(
'title' => $title,
'content' => $content
);
$data = array();
// Create a meeting post in WordPress
$post_id = $this->create_zoom_meeting_post($args);
// Create a meeting on zoom via rest api
$data = $this->create_zoom_meeting_from_post($post_id, $request);
if (is_object($data)) {
$data = (array)$data;
}
$data['post_id'] = $post_id;
if (is_array($data) && sizeof($data)) {
return new WP_REST_Response($data, 200);
}
return new WP_Error('cant-create', __('message', 'text-domain'), array('status' => 500));
}
创建WordPress Post用wp_insert_post即可。
public function create_zoom_meeting_post($args) {
$post_title = sanitize_text_field($args['title']);
$post_content = sanitize_post($args['content']);
return wp_insert_post(array(
'post_title' => $post_title,
'post_content' => $post_content,
'post_status' => 'publish',
'post_type' => 'zoom-meetings'
));
}
创建zoom meeting的代码略微复杂一些,我们直接重用插件的功能——class-zvc-admin-post-type.php
里的function save_metabox( $post_id, $post )
public function create_zoom_meeting_from_post($post_id, $request) {
if (!current_user_can('edit_post', $post_id)) {
return;
}
// Check if not an autosave.
if (wp_is_post_autosave($post_id)) {
return;
}
// Check if not a revision.
if (wp_is_post_revision($post_id)) {
return;
}
$pwd = '';
$duration_hour = sanitize_text_field($request->get_param('option_duration_hour'));
$duration_minutes = sanitize_text_field($request->get_param('option_duration_minutes'));
$duration = !empty($duration_hour) || !empty($duration_minutes) ? vczapi_convert_to_minutes($duration_hour, $duration_minutes): 45;
$create_meeting_arr = array(
'userId' => sanitize_text_field($request->get_param('userId')),
'meeting_type' => absint(sanitize_text_field($request->get_param('meeting_type'))),
'start_date' => sanitize_text_field($request->get_param('start_date')),
'timezone' => sanitize_text_field($request->get_param('timezone')),
'duration' => $duration,
'password' => $pwd,
'disable_waiting_room' => sanitize_text_field($request->get_param('disable_waiting_room')),
'meeting_authentication' => sanitize_text_field($request->get_param('meeting_authentication')),
'option_host_video' => sanitize_text_field($request->get_param('option_host_video')),
'option_auto_recording' => sanitize_text_field($request->get_param('option_auto_recording')),
'alternative_host_ids' => sanitize_text_field($request->get_param('alternative_host_ids')),
);
$create_meeting_arr['join_before_host'] = sanitize_text_field($request->get_param('join_before_host'));
$create_meeting_arr['option_participants_video'] = sanitize_text_field($request->get_param('option_participants_video'));
$create_meeting_arr['option_mute_participants'] = sanitize_text_field($request->get_param('option_mute_participants'));
$create_meeting_arr['site_option_logged_in'] = sanitize_text_field($request->get_param('option_logged_in'));
$create_meeting_arr['site_option_browser_join'] = sanitize_text_field($request->get_param('option_browser_join'));
$create_meeting_arr['site_option_enable_debug_log'] = sanitize_text_field($request->get_param('option_enable_debug_log'));
update_post_meta($post_id, '_meeting_fields', $create_meeting_arr);
update_post_meta($post_id, '_vczapi_meeting_type', 'meeting');
try {
//converted saved time from the timezone provided for meeting to UTC timezone so meetings can be better queried
$savedDateTime = new DateTime($create_meeting_arr['start_date'], new DateTimeZone($create_meeting_arr['timezone']));
$startDateTimezone = $savedDateTime->setTimezone(new DateTimeZone('UTC'));
update_post_meta($post_id, '_meeting_field_start_date_utc', $startDateTimezone->format('Y-m-d H:i:s'));
} catch (Exception $e) {
update_post_meta($post_id, '_meeting_field_start_date_utc', $e->getMessage());
}
//Create Zoom Meeting Now
$meeting_id = get_post_meta($post_id, '_meeting_zoom_meeting_id', true);
$post = get_post($post_id);
return $this->create_zoom_meeting($post, $create_meeting_arr);
}
public function create_zoom_meeting($post, $create_meeting_arr) {
$mtg_param = Zoom_Video_Conferencing_Admin_Meetings::prepare_create($create_meeting_arr, $post);
$meeting_created = json_decode(zoom_conference()->createAMeeting($mtg_param));
if (empty($meeting_created->code)) {
update_post_meta($post->ID, '_meeting_zoom_details', $meeting_created);
update_post_meta($post->ID, '_meeting_zoom_join_url', $meeting_created->join_url);
update_post_meta($post->ID, '_meeting_zoom_start_url', $meeting_created->start_url);
update_post_meta($post->ID, '_meeting_zoom_meeting_id', $meeting_created->id);
} else {
//Store Error Message
update_post_meta($post->ID, '_meeting_zoom_details', $meeting_created);
}
return $meeting_created;
}
create_zoom_meeting_from_post
函数主要是收集数据,然后将信息写入postmeta里,最后调用create_zoom_meeting
函数,该函数会调用Zoom Rest API到zoom里创建meeting。
用Postman发送请求创建zoom会议
因为这个REST API限定了权限,所以要使用管理员账户创建Application Passwords来访问,这里不赘述。
请求地址:https://www.example.com/wp-json/zoom/v1/meetings
要发送的数据:
title:会议名称
content:会议介绍
option_duration_hour:0
option_duration_minutes:40
userId:【替换成自己的user ID】
start_date:2023-07-30 15:00
timezone:America/Panama
//disable_waiting_room:
//meeting_authentication:
//option_host_video:
option_auto_recording:cloud
//alternative_host_ids:
join_before_host:1
//option_participants_video:
//option_mute_participants:
//option_logged_in:
//option_browser_join:
//option_enable_debug_log: