Skip to content

Instantly share code, notes, and snippets.

@stek29
Last active November 18, 2017 23:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stek29/d22d1f34d1009a391d3d80cef6677b8c to your computer and use it in GitHub Desktop.
Save stek29/d22d1f34d1009a391d3d80cef6677b8c to your computer and use it in GitHub Desktop.
<?php
if (!isset($_REQUEST)) {
return;
}
class ConfigSomeGroup {
public function shouldDelete($object) {
/* delete cyrillic text */
$comment_text = $object->text;
$regex = '/[\x{430}-\x{44F}\x{451}]/iu';
if (preg_match($regex, $comment_text)) {
if (isset($object->reply_to_user)) {
$reply_to = $object->reply_to_user;
$name = '';
if ($reply_to > 0) {
$request_params = array(
'user_ids' => $reply_to,
'access_token' => self::$vk_adm_token,
'v' => '5.0'
);
$get_params = http_build_query($request_params);
$ans = json_decode(file_get_contents('https://api.vk.com/method/users.get?' . $get_params));
if (isset($ans->response[0]->first_name))
$name = $ans->response[0]->first_name;
}
$comment_text = str_replace($name, '', $comment_text);
if (!preg_match($regex, $comment_text))
return false;
}
return true;
}
return false;
}
public static $vk_secret = 'VK_SECRET';
public static $vk_confirm = 'VK_CONFIRM_TOKEN';
public static $vk_adm_token = 'VK_TOKEN_OF_SOME_ADMIN';
public static $tg_token = 'TG_BOT_TOKEN';
public static $tg_channel = 'TG_CHANNEL_ID';
}
$config = array(
129440544 => new ConfigSomeGroup()
);
function tg_execute($token, $method, $params) {
$tget_params = http_build_query($params);
$tg_api_call = json_decode(
file_get_contents(
'https://api.telegram.org/bot'
. $token
. '/' . $method
. '?' . $tget_params
)
);
if (
(isset($tg_api_call->ok) and !$tg_api_call->ok)
or !isset($tg_api_call->result)
) {
syslog(LOG_WARNING, "TG ERR: ".json_encode($tg_api_call));
return NULL;
}
return $tg_api_call->result;
}
$data = json_decode(file_get_contents('php://input'));
if (!(isset($data->type) and isset($data->group_id) and isset($config[$data->group_id]))) {
echo "error";
exit(1);
}
$cur_config = $config[$data->group_id];
if (isset($data->debug) and $data->debug == "UIWGRNDBG") {
var_dump($cur_config);
var_dump($cur_config::$vk_secret);
var_dump($cur_config::$tg_channel);
var_dump($data);
var_dump($config);
}
if ($data->type == 'confirmation') {
echo $cur_config::$vk_confirm;
} else if (!(isset($data->secret) and $cur_config::$vk_secret == $data->secret)) {
echo "error";
} else {
switch ($data->type) {
case 'wall_reply_new':
case 'wall_reply_edit':
case 'photo_comment_new':
if ($cur_config->shouldDelete($data->object)) {
$request_params = array(
'comment_id' => $data->object->id,
'owner_id' => -($data->group_id),
'access_token' => $cur_config::$vk_adm_token,
'v' => '5.0'
);
$get_params = http_build_query($request_params);
$api_call = file_get_contents('https://api.vk.com/method/wall.deleteComment?' . $get_params);
}
echo "ok";
break;
case 'wall_post_new':
if (!isset($data->object->from_id) or !isset($data->object->owner_id)) {
syslog(LOG_WARNING, "Post with no from/owner: ".json_encode($data));
echo "ok";
break;
}
if ($data->object->from_id != $data->object->owner_id) {
syslog(LOG_INFO, "From/Owner mismatch. Suggested post?: ".json_encode($data));
echo "ok";
break;
}
if (!empty($data->object->text)) {
$post_text = $data->object->text;
} else {
$post_text = NULL;
}
if (isset($data->object->attachments)) {
$attachments = $data->object->attachments;
$nudes = [];
foreach ($attachments as $attach) {
if (isset($attach->type) and ($attach->type == 'photo') and isset($attach->photo)) {
$photo = $attach->photo;
if (isset($photo->photo_2560)) $photo_url = $photo_2560; else
if (isset($photo->photo_1280)) $photo_url = $photo->photo_1280; else
if (isset($photo->photo_807 )) $photo_url = $photo->photo_807 ; else
if (isset($photo->photo_604 )) $photo_url = $photo->photo_604 ; else
if (isset($photo->photo_130 )) $photo_url = $photo->photo_130 ; else
if (isset($photo->photo_75 )) $photo_url = $photo->photo_75 ; else {
syslog(LOG_WARNING, "Can't find photo size: ".json_encode($photo));
continue;
}
$nudes[] = array(
'type' => 'photo',
'media' => $photo_url
);
}
}
if (count($nudes) == 1) {
$tg_params = array(
'chat_id' => $cur_config::$tg_channel
);
if ($nudes[0]['type'] == 'photo') {
$method = 'sendPhoto';
$tg_params['photo'] = $nudes[0]['media'];
if ($post_text != NULL && strlen($post_text) < 200) {
$tg_params['caption'] = $post_text;
$post_text = NULL;
}
}
if (isset($method)) {
$tg_api_res = tg_execute(
$cur_config::$tg_token,
$method,
$tg_params
);
if (!isset($tg_api_res->message_id)) {
syslog(LOG_WARNING, "Can't get message_id: ".json_encode($tg_api_res));
} else {
$reply_to_id = $tg_api_res->message_id;
}
}
} elseif (count($nudes) > 1) {
$tg_api_res = tg_execute(
$cur_config::$tg_token,
'sendMediaGroup',
array(
'chat_id' => $cur_config::$tg_channel,
'media' => json_encode($nudes)
)
);
if (!isset($tg_api_res[0]->message_id)) {
syslog(LOG_WARNING, "Can't get message_id: "
. json_encode($tg_api_call->result));
} else {
$reply_to_id = $tg_api_res[0]->message_id;
}
}
if (!empty($post_text)) {
$tg_params = array(
'chat_id' => $cur_config::$tg_channel,
'text' => $post_text
);
if (isset($reply_to_id)) {
$tg_params['reply_to_message_id'] = $reply_to_id;
$tg_params['disable_notification'] = true;
}
tg_execute(
$cur_config::$tg_token,
'sendMessage',
$tg_params
);
}
syslog(LOG_INFO, "Processed attachments: ".json_encode($attachments));
}
echo "ok";
break;
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment