Skip to content

Instantly share code, notes, and snippets.

@semasping
Created September 12, 2017 10:27
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 semasping/2f7fd7b378c85a5661c81e29937eb368 to your computer and use it in GitHub Desktop.
Save semasping/2f7fd7b378c85a5661c81e29937eb368 to your computer and use it in GitHub Desktop.
GolosProcessCreatePost
<?php
namespace App\Jobs;
use App\BotUserNot;
use App\GolosBlackListForBots;
use App\GolosBotPost;
use App\semas\AdminNotify;
use GrapheneNodeClient\Tools\Transliterator;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Support\Facades\Redis;
use Jenssegers\Date\Date;
use Telegram\Bot\Exceptions\TelegramResponseException;
use Telegram\Bot\Laravel\Facades\Telegram;
class GolosProcessCreatePost implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected $post;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct($post)
{
$this->post = $post;
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
//dump($this->post); //// подумать как учесть bot_name так как один чат на всех ботов. выходит если этому боту отправил - то нужно смотреть нет ли другого бота для этого же чата....
$post = $this->post;
$jd = json_decode($this->post['json_metadata']);
echo 'post' . "\n";
if (isset($jd->tags)) {
echo 'tags' . "\n";
$tags = $jd->tags;
if ( ! in_array($post['parent_permlink'], $tags)) {
$tags[] = $post['parent_permlink'];
}
dump($tags);
$chats_id = BotUserNot::whereIn('tag', $tags)->get();
$message_sent = [];
foreach ($chats_id as $chat) {
echo 'chat_id=' . $chat->chat_id . "\n";
$chat_id = $chat->chat_id;
if ( ! isset($message_sent[$chat_id])) {
$post_code = $chat_id . $post['author'] . $post['permlink'];
if (GolosBotPost::where('chat_id', $chat_id)->where('post', $post_code)->where('bot_name', $chat->bot_name)->count() <= 0) {
echo 123 . "\n";
if (isset($chat->tag)) {
echo 'post_code' . $post_code . "\n";
if ($this->checkBlackList($chat, $tags, 'tag')) {
$this->PostProcessed($chat_id, $post_code, $chat);
$message_sent[$chat_id] = 'blacklist';
continue;
}
if ($this->checkBlackList($chat, [str_replace('@','', $post['author'])], 'author')) {
$this->PostProcessed($chat_id, $post_code, $chat);
$message_sent[$chat_id] = 'blacklist';
continue;
}
$tag = $chat->tag;
if (str_contains($tag, 'ru--')) {
$tag = Transliterator::decode($tag, Transliterator::LANG_RU);
}
echo 'trans tag ' . $tag . "\n";
$other_tags = $this->decodeTags($tags);
$author = $post['author'];
$title = $post['title'];
$main_tag = $post['parent_permlink'];
$url_post = 'https://golos.io/' . $main_tag . '/@' . $author . '/' . $post['permlink'] . '/';
$url_author = 'https://golos.io/@' . $author;
$url_tag = 'https://golos.io/created/' . $chat->tag . '';
$navigation = "";
if (isset($post['timestamp'])) {
$date = Date::parse($post['timestamp'])->format('d F H:i') . ' (GMT)';
} else {
$date = '';
}
try {
$message_id = Telegram::setAccessToken(getenv('TELEGRAM_BOT_TOKEN_' . $chat->bot_name))->sendMessage([
'chat_id' => $chat_id,
'parse_mode' => 'Markdown',
//'text' => 'Статья c тегом: #' . $tag . '' . ' от @' . $author . ' ' . "\n" . '[' . $title . '](' . $url_post . ') ' . "\n" . 'bot.v:0.62',
'text' => '[' . $title . '](' . $url_post . ') ' . "\n" . 'Статья c тегом: [#' . $tag . '](' . $url_tag . ')' . ' от [@' . $author . '](' . $url_author . ') ' . $date . "\n" . 'Теги: ' . $other_tags . "\n" . 'bot.v:0.95',
]);
if ($message_id) {
//echo 'Send';
//echo 'message_id=' . $message_id . "\n";
//$mess = json_decode($message_id);
//echo "mess".$mess;
//AdminNotify::send((json_decode($message_id)->message_id));
$message_sent[$chat_id] = $message_id;
//Redis::set("$post_code", $post_code);
$this->PostProcessed($chat_id, $post_code, $chat);
}
} catch (TelegramResponseException $e) {
echo $e->getMessage();
$errorData = $e->getResponseData();
if ($errorData['ok'] === false) {
Telegram::setAccessToken(getenv('TELEGRAM_BOT_TOKEN'))->sendMessage([
'chat_id' => '147893636',
'text' => 'There was an error for a user. ' . $errorData['error_code'] . ' 2 ' . $errorData['description'] . " bot name: $chat->bot_name, chat_id =$chat->chat_id, full print_r " . print_r($errorData,
true),
]);
if (str_contains($errorData['description'], 'bot was blocked')) {
}
}
}
// $messages[$message_id][] = $tag;
}
} else {
echo 'already sent for chat ' . $chat_id . ' post: ' . $post_code . "\n";
}
}
}
/* unset($post['body']);
AdminNotify::send(print_r($post,true));
if ($post['parent_permlink']=='goldvoice'){
AdminNotify::send(print_r($post,true));
AdminNotify::send(print_r($post,true));
AdminNotify::send(print_r($post,true));
AdminNotify::send(print_r($tags,true));
}*/
//dd($chats_id, $tags);
}
echo 123;
}
function decodeTags($tags)
{
foreach ($tags as $tag) {
if (str_contains($tag, 'ru--')) {
$tag = Transliterator::decode($tag, Transliterator::LANG_RU);
}
$dTags[] = '#' . $tag;
}
return implode(', ', $dTags);
}
private function checkBlackList($chat, $tags, $type)
{
echo 321 . $type . "\n";
$black = GolosBlackListForBots::where('bot_name', $chat->bot_name)
->where('chat_id', '=', $chat->chat_id)
->wherein('text_for_block', $tags)
->where('type', $type);
if ($black->count() > 0) {
echo 321 . ' r true ' . "\n";
return true;
}else{
echo 321 . ' r false ' . "\n";
return false;
}
}
/**
* @param $chat_id
* @param $post_code
* @param $chat
*/
private function PostProcessed($chat_id, $post_code, $chat)
{
$gbp = new GolosBotPost();
$gbp->chat_id = $chat_id;
$gbp->post = $post_code;
$gbp->bot_name = $chat->bot_name;
$gbp->save();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment