Textpattern CMS support forum
You are not logged in. Register | Login | Help
- Topics: Active | Unanswered
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
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 ©=1
to its edit URL. For some reason (that I can’t remember), this “copy” link has never been rendered public.
Offline
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
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
Offline
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
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
Re: Understanding "article_posting" callback
hm, I thought, that this concerns article_save
, so I have to rethink this plugin.
Offline
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