Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2019-08-29 19:33:02

whocarez
Plugin Author
From: Germany/Ukraine
Registered: 2007-10-08
Posts: 305
Website GitHub Twitter

Understanding "article_posting" callback

Hello,
I’m actually working on a very simple plugin for autoposting to Telegram. Thus I used register_callback('wcz_telegram', 'article_posted'); to trigger this function every time I post a new article. In principle it is working, but today I recognized, that if I use the copy function for copying an older article, than “article_posting” is also triggered and after that, when I was really posting this article, nothing happened, because it had this “already posted” status.
So, the maybe stupid question is, how can I prevent triggering my function, when I only make a copy of an older article, where the copy has a status of “1”.

Actually the plugin code looks like that:

if (txpinterface == 'admin')
{
         add_privs('wcz_telegram_post', '1');
	register_callback('wcz_telegram', 'article_posted');
}

// Register tags.
Txp::get('\Textpattern\Tag\Registry')
    ->register('wcz_telegram');

function wcz_telegram() {

/* begin borrowed from arc_twitter - Andy Carter */
    $article_id = empty($GLOBALS['ID']) ? gps('ID') : $GLOBALS['ID'];
    if (!empty($article_id)) {

	include_once txpath.'/publish/taghandlers.php';

	$article = safe_row("ID, Title, Excerpt, Posted, Keywords", 'textpattern',
	    "ID={$article_id} AND Status=4 AND now()>=Posted");
    }
/* end borrowed from arc_twitter */


	$article_url = permlinkurl($article);
	$title = $article['Title'];
	$excerpt = $article['Excerpt'];
	$keywords = $article['Keywords'];


	$wcz_telegram_token = 'bot_token';

	$wcz_telegram_chatid = 'chatid';

	$wcz_telegram_rhash = 'hashxyz';
	$wcz_telegram_iv = '1';
	$wcz_telegram_debug = '1';

    if ($wcz_telegram_iv == '1') {
	$article_url = "https://t.me/iv?url=".$article_url."&rhash=".$wcz_telegram_rhash;
    }


//  escape Markdown formatting character "_"
    $article_url = str_replace("_","\_",$article_url);

//  prepare keywords
    $keywords = explode(",",$keywords);
    foreach ($keywords as &$keyword) {
        $keyword = preg_replace('/\s+/', '',mb_convert_case($keyword, MB_CASE_TITLE));
    }
    $keywords = implode(" ",preg_filter('/^/', '#', $keywords));

// 4096 maximum post length

    $excerptlen = 4080 - strlen($title) - strlen($keywords) - strlen($article_url);

    if (strlen($excerpt) > $excerptlen) {
        $pos=strpos($excerpt, ' ', $excerptlen);
        $excerpt = '"'.substr($excerpt,0,$pos).'" ...';
    }



    $message = "*".$title."*\n\n".$excerpt."\n\n".$keywords."\n\n".$article_url;

    $url = 'https://api.telegram.org/bot'.$wcz_telegram_token.'/sendMessage?chat_id='.$wcz_telegram_chatid.'&parse_mode=Markdown&text=';
    $url .= urlencode($message);


// some debug stuff
    if ($wcz_telegram_debug == "1") {
        file_put_contents(txpath."/tmp/telegram_request.txt","Sent URL: \n".$url."\n\nPrepared message:\n".$message."\n\nExcerpt length:\n".$excerptlen."\n\nCurl output:\n");
    }


    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// some debug stuff
    if ($wcz_telegram_debug == "1") {
        curl_setopt($ch, CURLOPT_VERBOSE, true);
	$curl_verbose = fopen(txpath.'/tmp/telegram_request.txt', 'a+');
        curl_setopt($ch, CURLOPT_STDERR, $curl_verbose);
    }
    $response = curl_exec($ch);
    curl_close($ch);


}

Last edited by whocarez (2019-08-29 19:34:13)

Offline

#2 2019-08-29 21:48:29

etc
Developer
Registered: 2010-11-11
Posts: 5,239
Website GitHub

Re: Understanding "article_posting" callback

This does not quite answer your question, but IIRC, you can copy an article (without actually publishing it, as with duplicate) by appending &copy=1 to its edit URL. For some reason (that I can’t remember), this “copy” link has never been rendered public.

Offline

#3 2019-08-29 22:00:31

whocarez
Plugin Author
From: Germany/Ukraine
Registered: 2007-10-08
Posts: 305
Website GitHub Twitter

Re: Understanding "article_posting" callback

I do not really often use this Copy function, but I was surprised that something was posted to Telegram, after doing only this copy, which I didn’t expected. And as far as I understand this callback, this shouldn’t happen.

Offline

#4 2019-08-29 22:07:45

etc
Developer
Registered: 2010-11-11
Posts: 5,239
Website GitHub

Re: Understanding "article_posting" callback

I’m not sure we are speaking about the same “copy”. When you click “Duplicate” button, a copy is posted (with Draft status), triggering the callback. To avoid it, don’t click “Duplicate” but add copy=1 to URL of the article being edited and access this “copy” URL. All article data will then be copied, but the new article will not be created until you hit “Publish” button.

Offline

#5 2019-08-29 22:12:55

etc
Developer
Registered: 2010-11-11
Posts: 5,239
Website GitHub

Re: Understanding "article_posting" callback

whocarez wrote #319191:

And as far as I understand this callback, this shouldn’t happen.

Since you can duplicate multiple articles at once (via multiselect widget), firing the callback can actually make sense.

Offline

#6 2019-08-29 22:13:39

whocarez
Plugin Author
From: Germany/Ukraine
Registered: 2007-10-08
Posts: 305
Website GitHub Twitter

Re: Understanding "article_posting" callback

We are talking about the same one. So I my understanding of article_posting is wrong. I thought this is an event, which is called only when I push the publish button.

Offline

#7 2019-08-29 22:16:01

etc
Developer
Registered: 2010-11-11
Posts: 5,239
Website GitHub

Re: Understanding "article_posting" callback

whocarez wrote #319194:

We are talking about the same one. So I my understanding of article_posting is wrong. I thought this is an event, which is called only when I push the publish button.

I guess it’s fired when an article is added to db, whatever status.

Offline

#8 2019-08-29 22:19:55

whocarez
Plugin Author
From: Germany/Ukraine
Registered: 2007-10-08
Posts: 305
Website GitHub Twitter

Re: Understanding "article_posting" callback

hm, I thought, that this concerns article_save, so I have to rethink this plugin.

Offline

#9 2019-09-05 22:05:30

whocarez
Plugin Author
From: Germany/Ukraine
Registered: 2007-10-08
Posts: 305
Website GitHub Twitter

Re: Understanding "article_posting" callback

Reading documentations helps sometimes ;-)

article_posted
  • When it occurs: Immediately after article creation.

This plugin looks now like this: wcz_telegram

Offline

Board footer

Powered by FluxBB