Nieuws koppelen aan Twitter via UDT en Events [update]
Moderator: velden
Re: Nieuws koppelen aan Twitter via UDT en Events [update]
Ah, dank je. Ga ik die leeg maken. Had dan overigens wel een tweet verwacht, maar dat staat daar los van schat ik in.
Gregor
Gregor
Re: Nieuws koppelen aan Twitter via UDT en Events [update]
@Arnoud: de string leeggemaakt, maar een tweet komt er nog niet uit. Ik heb in de code een aantal echo-statements gezet om te zien wat de waarde van een variabele is en heb daardoor gezien dat de functie 'fetch_cgblog_url' niet lijkt te worden aangeroepen.
Na submit van een blog-artikel, verschijnen in het scherm de volgende echo's
Heb jij een idee waarom ik de echo in de functie niet zie bijv.?
Gregor
Na submit van een blog-artikel, verschijnen in het scherm de volgende echo's
Wat verder opvalt, is dat cgblog niet terugkomt met een blog-overzicht, maar die, het overzicht dus, alleen maar weer via het menu zichtbaar kan worden gemaakt.cgblog_id = 273 title = Saildrive met leven erin aliased_title = Saildrive-met-leven-erin cgblog_url = cgblog_id = 273
Code: Select all
/*******************************************************
* about
*******************************************************/
/*
This UDT can be used to automatically post a Twitter message when a news article is added.
The functionality is based on the Twitter and News module.
- First you need to install and setup the News and Twitter module.
- After you need to create a UDT with this code, for example you can call it: 'news_twitter_update_status'
- At last you need to connect this UDT to the 'NewsArticleAdded' event.
This can be done via Extensions -> Event Manager.
[s]! Currently this UDT only works when pretty urls are enabled.
It seems that the bit.ly url shortner ($twitter->shortenUrls($link)) has some problems with unfriendly urls.
Querystring parameters &var=1 are converted to &=1 which will break the link.[/s]
==
update: 16 Dec 2011
==
- This UDT is now compatible with cmsms 1.10
- Works with and without pretty urls bit.ly problem seems solved
- Added the ability to shortenurl's
- Added support for the news_url field, if you use these it's best to turnshortenurl's off
config:
- specify: $detailpage (page_id) of the news detail view.
- specify: $shortenurl (true|false) will use the bit.ly url shortner or not.
*/
/*******************************************************
* globals/objects
*******************************************************/
$modops = cmsms()->GetModuleOperations();
// echo ' modops = ' . $modops;
$twitter = $modops->get_module_instance('Twitter');
// echo ' twitter = ' . $twitter;
$cgblog = $modops->get_module_instance('CGBlog');
// echo ' cgblog = ' . $cgblog;
/*******************************************************
* config
*******************************************************/
/* debug (bool) */
$debug = false;
/* custom (string) */
$detailpage = '';
/* shortenurl (bool) */
$shortenurl = false;
/*******************************************************
* variables
*******************************************************/
/* CGBlog article related (string) */
$cgblog_id = $params['cgblog_id'];
echo 'cgblog_id = ' . $cgblog_id;
$title = $params['title'];
echo ' title = ' . $title;
/* added to link (array) */
$link['articleid'] = $params['cgblog_id'];
/*******************************************************
* program
*******************************************************/
$aliased_title = munge_string_to_url($title);
echo ' aliased_title = ' . $aliased_title;
echo ' cgblog_url = ' . $cgblog_url;
echo ' cgblog_id = ' . $cgblog_id;
$cgblog_url = fetch_cgblog_url($cgblog_id);
echo ' cgblog_url-1 = ' . $cgblog_url;
if ($cgblog_url != '') {
$prettyurl = $cgblog_url;
} else {
$prettyurl = 'logboek/' . $cgblog_id.'/'.$detailpage."/$aliased_title";
}
echo ' prettyurl = ' . $prettyurl;
$link = $cgblog->CreateLink($cgblog_id, 'detail', $detailpage, '', $link,'', true, false, '', true, $prettyurl);
if ($shortenurl) {
$shortlink = $twitter->shortenUrls($link);
$message = $title. ' '. $shortlink;
} else {
$message = $title. ' '. $link;
}
if ($debug) {
print "<pre>twitter: $message status: $params[status] link: $link title: $title prettyurl: $prettyurl \n</pre>";
} else {
if ($params[status] == 'published') {
echo "debug";
$twitter->updateStatus($message, false);
}
}
/*******************************************************
* subroutines
*******************************************************/
/**
* fetch the $cgblog_url by $cgblog_id
*
* @param string $cgblog_id
* @return string $cgblog_url
*
*/
function fetch_cgblog_url($cgblog_id) {
echo " in function ";
$db = cmsms()->GetDb();
$query = "SELECT url
FROM ". cms_db_prefix(). "module_cgblog
WHERE cgblog_id = ?";
$cgblog_url = $db->GetOne($query, array($cgblog_id));
echo ' cgblog_url-function = ' . $cgblog_url;
return $cgblog_url;
}
Gregor
Re: Nieuws koppelen aan Twitter via UDT en Events [update]
Klein stapje verder. Als ik de functie er even uithaal en de variable '' maak (dus leeg), dan verschijnt er wel een tweet en komt cgblog weer terug met het blog-overzicht. Mijn voorzichtige conclusie is, dat ie faalt bij de aanroep van de functie. Zie ook de echo in de functie die niet getoond wordt.
Gregor
Gregor
Re: Nieuws koppelen aan Twitter via UDT en Events [update]
Gevonden 
Het lijkt dat de function niet achteraan mag staan; na het verplaatsen naar de parameter-sectie, werkte het wel
In de code verder een aanpassing gedaan dat als $detailpage = '' er geen extra / wordt geplaatst waardoor er // in de url zouden komen.
De code om deze UDT te gebruiken i.c.m. CGBlog is:
Ben wel nieuwsgierig waarom de plaats van de functie zo bepalend is, dus waarom deze niet aan het einde mag staan.
Gregor

Het lijkt dat de function niet achteraan mag staan; na het verplaatsen naar de parameter-sectie, werkte het wel

In de code verder een aanpassing gedaan dat als $detailpage = '' er geen extra / wordt geplaatst waardoor er // in de url zouden komen.
De code om deze UDT te gebruiken i.c.m. CGBlog is:
Code: Select all
/*******************************************************
* about
*******************************************************/
/*
This UDT can be used to automatically post a Twitter message when a news article is added.
The functionality is based on the Twitter and News module.
- First you need to install and setup the News and Twitter module.
- After you need to create a UDT with this code, for example you can call it: 'news_twitter_update_status'
- At last you need to connect this UDT to the 'NewsArticleAdded' event.
This can be done via Extensions -> Event Manager.
[s]! Currently this UDT only works when pretty urls are enabled.
It seems that the bit.ly url shortner ($twitter->shortenUrls($link)) has some problems with unfriendly urls.
Querystring parameters &var=1 are converted to &=1 which will break the link.[/s]
==
update: 16 Dec 2011
==
- This UDT is now compatible with cmsms 1.10
- Works with and without pretty urls bit.ly problem seems solved
- Added the ability to shortenurl's
- Added support for the news_url field, if you use these it's best to turnshortenurl's off
config:
- specify: $detailpage (page_id) of the news detail view.
- specify: $shortenurl (true|false) will use the bit.ly url shortner or not.
*/
/*******************************************************
* globals/objects
*******************************************************/
$modops = cmsms()->GetModuleOperations();
// echo ' modops = ' . $modops;
$twitter = $modops->get_module_instance('Twitter');
// echo ' twitter = ' . $twitter;
$cgblog = $modops->get_module_instance('CGBlog');
// echo ' cgblog = ' . $cgblog;
/*******************************************************
* config
*******************************************************/
/* debug (bool) */
$debug = false;
/* custom (string) */
$detailpage = '';
/* shortenurl (bool) */
$shortenurl = false;
/*******************************************************
* variables
*******************************************************/
/* CGBlog article related (string) */
$cgblog_id = $params['cgblog_id'];
// echo 'cgblog_id = ' . $cgblog_id;
$title = $params['title'];
// echo ' title = ' . $title;
/* added to link (array) */
$link['articleid'] = $params['cgblog_id'];
/*******************************************************
* subroutines
*******************************************************/
/**
* fetch the $cgblog_url by $cgblog_id
*
* @param string $cgblog_id
* @return string $cgblog_url
*
*/
function fetch_cgblog_url($cgblog_id){
$db = cmsms()->GetDb();
$query = "SELECT url
FROM ". cms_db_prefix(). "module_cgblog
WHERE cgblog_id = ?";
$cgblog_url = $db->GetOne($query, array($cgblog_id));
return $cgblog_url;
}
/*******************************************************
* program
*******************************************************/
$aliased_title = munge_string_to_url($title);
// echo ' aliased_title = ' . $aliased_title;
// echo ' cgblog_url = ' . $cgblog_url;
// echo ' cgblog_id = ' . $cgblog_id;
$cgblog_url = fetch_cgblog_url($cgblog_id);
// $cgblog_url = '';
if ($cgblog_url != '') {
$prettyurl = $cgblog_url;
} else {
// if $detailpage == '' we don't want an extra /
if ($detailpage != '') {
$prettyurl = 'logboek/' . $cgblog_id.'/'.$detailpage."/$aliased_title";
} else {
$prettyurl = 'logboek/' . $cgblog_id."/$aliased_title";
}
}
// echo ' prettyurl = ' . $prettyurl;
$link = $cgblog->CreateLink($cgblog_id, 'detail', $detailpage, '', $link,'', true, false, '', true, $prettyurl);
if ($shortenurl) {
$shortlink = $twitter->shortenUrls($link);
$message = $title. ' '. $shortlink;
} else {
$message = $title. ' '. $link;
}
if ($debug) {
print "<pre>twitter: $message status: $params[status] link: $link title: $title prettyurl: $prettyurl \n</pre>";
} else {
if ($params[status] == 'published') {
$twitter->updateStatus($message, false);
}
}
Gregor
Re: Nieuws koppelen aan Twitter via UDT en Events [update]
Ik heb een toevoeging aan het script gemaakt om het mogelijk te maken een hashtag aan je tweet toe te voegen.
Gregor
Code: Select all
/*******************************************************
* about
*******************************************************/
/*
This UDT can be used to automatically post a Twitter message when a news article is added.
The functionality is based on the Twitter and News module.
- First you need to install and setup the News and Twitter module.
- After you need to create a UDT with this code, for example you can call it: 'news_twitter_update_status'
- At last you need to connect this UDT to the 'NewsArticleAdded' event.
This can be done via Extensions -> Event Manager.
[s]! Currently this UDT only works when pretty urls are enabled.
It seems that the bit.ly url shortner ($twitter->shortenUrls($link)) has some problems with unfriendly urls.
Querystring parameters &var=1 are converted to &=1 which will break the link.[/s]
==
update: 16 Dec 2011
==
- This UDT is now compatible with cmsms 1.10
- Works with and without pretty urls bit.ly problem seems solved
- Added the ability to shortenurl's
- Added support for the news_url field, if you use these it's best to turnshortenurl's off
config:
- specify: $detailpage (page_id) of the news detail view.
- specify: $shortenurl (true|false) will use the bit.ly url shortner or not.
*/
/*******************************************************
* globals/objects
*******************************************************/
$modops = cmsms()->GetModuleOperations();
// echo ' modops = ' . $modops;
$twitter = $modops->get_module_instance('Twitter');
// echo ' twitter = ' . $twitter;
$cgblog = $modops->get_module_instance('CGBlog');
// echo ' cgblog = ' . $cgblog;
/*******************************************************
* config
*******************************************************/
/* debug (bool) */
$debug = false;
/* custom (string) */
$detailpage = '';
/* shortenurl (bool) */
$shortenurl = false;
/* Prefix to use on all URLS from the blog module (string) */
$cgblog_page = 'logboek';
/* Extra parameter to add with tweet, f.i. a #xyz */
$extra_param = '#uisgebeatha';
/*******************************************************
* variables
*******************************************************/
/* CGBlog article related (string) */
$cgblog_id = $params['cgblog_id'];
// echo 'cgblog_id = ' . $cgblog_id;
$title = $params['title'];
// echo ' title = ' . $title;
/* added to link (array) */
$link['articleid'] = $params['cgblog_id'];
/*******************************************************
* subroutines
*******************************************************/
/**
* fetch the $cgblog_url by $cgblog_id
*
* @param string $cgblog_id
* @return string $cgblog_url
*
*/
function fetch_cgblog_url($cgblog_id){
$db = cmsms()->GetDb();
$query = "SELECT url
FROM ". cms_db_prefix(). "module_cgblog
WHERE cgblog_id = ?";
$cgblog_url = $db->GetOne($query, array($cgblog_id));
return $cgblog_url;
}
/*******************************************************
* program
*******************************************************/
$aliased_title = munge_string_to_url($title);
// echo ' aliased_title = ' . $aliased_title;
// echo ' cgblog_url = ' . $cgblog_url;
// echo ' cgblog_id = ' . $cgblog_id;
$cgblog_url = fetch_cgblog_url($cgblog_id);
// $cgblog_url = '';
if ($cgblog_url != '') {
$prettyurl = $cgblog_url;
} else {
// if $detailpage == '' we don't want an extra /
if ($detailpage != '') {
$prettyurl = $cgblog_page . '/' . $cgblog_id.'/'.$detailpage."/$aliased_title";
} else {
$prettyurl = $cgblog_page . '/' . $cgblog_id."/$aliased_title";
}
}
// echo ' prettyurl = ' . $prettyurl;
$link = $cgblog->CreateLink($cgblog_id, 'detail', $detailpage, '', $link,'', true, false, '', true, $prettyurl);
if ($shortenurl) {
$shortlink = $twitter->shortenUrls($link);
$message = $title. ' '. $shortlink;
} else {
$message = $title. ' '. $link;
}
// add extra parameter, if entered one
if ($extra_param != '') {
$message = $message . ' ' . $extra_param;
}
if ($debug) {
print "<pre>twitter: $message status: $params[status] link: $link title: $title prettyurl: $prettyurl \n</pre>";
} else {
if ($params[status] == 'published') {
$twitter->updateStatus($message, false);
}
}
Re: Nieuws koppelen aan Twitter via UDT en Events [update]
----------
Last edited by deactivated010521 on Tue Mar 12, 2013 5:58 pm, edited 1 time in total.
Re: Nieuws koppelen aan Twitter via UDT en Events [update]
Dank je voor het compliment Arnoud.
Het fuzzy matching is wel een heel fraaie oplossing. De totale lengte van een tweet is dan wel een voorwaarde
Aanvullend, dat je in de tekst aangeeft met een # ervoor wat je als hashtag zou willen gebruiken. Maakt het misschien wel gecompliceerd.
Gregor
Het fuzzy matching is wel een heel fraaie oplossing. De totale lengte van een tweet is dan wel een voorwaarde

Aanvullend, dat je in de tekst aangeeft met een # ervoor wat je als hashtag zou willen gebruiken. Maakt het misschien wel gecompliceerd.
Gregor
Re: Nieuws koppelen aan Twitter via UDT en Events [update]
----------
Last edited by deactivated010521 on Tue Mar 12, 2013 5:58 pm, edited 1 time in total.
Re: Nieuws koppelen aan Twitter via UDT en Events [update]
Dat heb je best handig gedaan Arnoud
Goed hoor!
Ik zal 'm aanpassen voor CGBlog. Het zou overigens handig zijn, als dat onderscheid niet gemaakt hoeft worden, maar dat in de UDT kan worden afgevangen. Had in een andere post op het Engels-talige forum gevraagd hoe ik de naam van de blog in de url kan uitlezen; dan hoef je niet, zoals in mijn voorbeeld 'logboek' op te nemen, maar de variabele waar die naam instaat.
Gregor

Ik zal 'm aanpassen voor CGBlog. Het zou overigens handig zijn, als dat onderscheid niet gemaakt hoeft worden, maar dat in de UDT kan worden afgevangen. Had in een andere post op het Engels-talige forum gevraagd hoe ik de naam van de blog in de url kan uitlezen; dan hoef je niet, zoals in mijn voorbeeld 'logboek' op te nemen, maar de variabele waar die naam instaat.
Gregor
Re: Nieuws koppelen aan Twitter via UDT en Events [update]
UDT bijgewerkt zodat deze ook voor CGBlog werkt.
Code: Select all
/*******************************************************
* about
*******************************************************/
/*
This UDT can be used to automatically post a Twitter message when a news article is added.
The functionality is based on the Twitter and News module.
- First you need to install and setup the News and Twitter module.
- After you need to create a UDT with this code, for example you can call it: 'news_twitter_update_status'
- At last you need to connect this UDT to the 'NewsArticleAdded' event.
This can be done via Extensions -> Event Manager.
[s]! Currently this UDT only works when pretty urls are enabled.
It seems that the bit.ly url shortner ($twitter->shortenUrls($link)) has some problems with unfriendly urls.
Querystring parameters &var=1 are converted to &=1 which will break the link.[/s]
==
update: 16 Dec 2011
==
- This UDT is now compatible with cmsms 1.10
- Works with and without pretty urls bit.ly problem seems solved
- Added the ability to shortenurl's
- Added support for the news_url field, if you use these it's best to turnshortenurl's off
==
UPDATE: 4 Jan 2011
==
- Added the $extra_param configuration option
- Added the $hashtags configuration option
config:
- specify: $detailpage (page_id) of the news detail view.
- specify: $shortenurl (true|false) will use the bit.ly url shortner or not.
- specify: $extra_param (string) to add a default hashtag.
- specify: $hashtags (array) to modify the $params['title'] into a title with #hashtags, if $hashtags = array(); no replacement will take place
: for example: $hashtags = array('cup', 'foo', 'bar');
: "We have won the cup I repeat worldcup, well that is not my cupoftea" -> "We have won the #cup I repeat #worldcup that is not my #cupoftea"
*/
/*******************************************************
* globals/objects
*******************************************************/
$modops = cmsms()->GetModuleOperations();
// echo ' modops = ' . $modops;
$twitter = $modops->get_module_instance('Twitter');
// echo ' twitter = ' . $twitter;
$cgblog = $modops->get_module_instance('CGBlog');
// echo ' cgblog = ' . $cgblog;
/*******************************************************
* config
*******************************************************/
/* debug (bool) */
$debug = false;
/* custom (string) */
$detailpage = '';
/* shortenurl (bool) */
$shortenurl = true;
/* Prefix to use on all URLS from the blog module (string) */
$cgblog_page = $cgblog->GetPreference('urlprefix','cgblog');
/* Extra parameter to add with tweet, f.i. a #xyz */
$extra_param = '#uisgebeatha';
/* $hashtags (array) */
$hashtags = array('zeilen', 'wedstrijd', 'watersport');
/*******************************************************
* variables
*******************************************************/
/* CGBlog article related (string) */
$cgblog_id = $params['cgblog_id'];
// echo 'cgblog_id = ' . $cgblog_id;
$title = $params['title'];
// echo ' title = ' . $title;
/* added to link (array) */
$link['articleid'] = $params['cgblog_id'];
/*******************************************************
* subroutines
*******************************************************/
/**
* fetch the $cgblog_url by $cgblog_id
*
* @param string $cgblog_id
* @return string $cgblog_url
*
*/
function fetch_cgblog_url($cgblog_id){
$db = cmsms()->GetDb();
$query = "SELECT url
FROM ". cms_db_prefix(). "module_cgblog
WHERE cgblog_id = ?";
$cgblog_url = $db->GetOne($query, array($cgblog_id));
return $cgblog_url;
}
/*******************************************************
* program
*******************************************************/
$aliased_title = munge_string_to_url($title);
// echo ' aliased_title = ' . $aliased_title;
// echo ' cgblog_url = ' . $cgblog_url;
// echo ' cgblog_id = ' . $cgblog_id;
$cgblog_url = fetch_cgblog_url($cgblog_id);
// $cgblog_url = '';
if ($cgblog_url != '') {
$prettyurl = $cgblog_url;
} else {
// if $detailpage == '' we don't want an extra /
if ($detailpage != '') {
$prettyurl = $cgblog_page . '/' . $cgblog_id.'/'.$detailpage."/$aliased_title";
} else {
$prettyurl = $cgblog_page . '/' . $cgblog_id."/$aliased_title";
}
}
// echo ' prettyurl = ' . $prettyurl;
$link = $cgblog->CreateLink($cgblog_id, 'detail', $detailpage, '', $link,'', true, false, '', true, $prettyurl);
// title to hashed_title
$hashed_title = $title;
if (count($hashtags)) {
$hashtags_pattern = implode('|', $hashtags);
$words = explode(' ', $title);
$seen = array();
foreach ($words as $key => $word) {
if (preg_match("/$hashtags_pattern/i", $word) ) {
$stripped_word = preg_replace('/[^A-Za-z-]/','', $word);
if (strlen($word) == strlen($stripped_word)) {
if (! in_array($word, $seen) ) {
$seen[] = $word;
$hashed_title = preg_replace('~\b' . preg_quote($word, '~') . '\b~', '#$0', $hashed_title);
}
}
}
}
}
// strlen check of hashed_title - $extra_param
// math(140 - 22 - 11) = 107 ([max_twitter_length] - [: space $shortlink])
if (strlen($hashed_title) <= 107) {
$title = $hashed_title;
}
if ($shortenurl) {
$shortlink = $twitter->shortenUrls($link);
$message = $title. ' '. $shortlink;
} else {
$message = $title. ' '. $link;
}
// add extra parameter, if entered one
if ($extra_param != '') {
$message = $message . ' ' . $extra_param;
}
if ($debug) {
print "<pre>twitter: $message status: $params[status] link: $link title: $title prettyurl: $prettyurl \n</pre>";
} else {
if ($params[status] == 'published') {
$twitter->updateStatus($message, false);
}
}
Last edited by Gregor on Fri Jan 06, 2012 7:58 pm, edited 1 time in total.
Re: Nieuws koppelen aan Twitter via UDT en Events [update]
----------
Last edited by deactivated010521 on Tue Mar 12, 2013 5:58 pm, edited 1 time in total.
Re: Nieuws koppelen aan Twitter via UDT en Events [update]
Dank je Arnoud. Ik heb het aan bovenstaand code toegevoegd.
Gregor
Gregor
Re: Nieuws koppelen aan Twitter via UDT en Events [update]
Arnoud,
Weet je ook een manier (of is ie er al en zie ik die over het hoofd) om een nieuwscategorie uit te sluiten van doorzending naar twitter?
Weet je ook een manier (of is ie er al en zie ik die over het hoofd) om een nieuwscategorie uit te sluiten van doorzending naar twitter?
Re: Nieuws koppelen aan Twitter via UDT en Events [update]
Ik heb de twitter module geinstalleert en heb me in gelogt.
Daarna heb ik een Gebruikergedefinieerde tags aangemaakt met de laatste code van arnoud ik moest wel de <?php en ?> weg halen anders pakte hij niet de rest is presies zo als er staat.
Deze heb ik gekoppelt in Gebeurtenissen Beheer aan NewsArticleAdded toegevoegt.
Vervolgens heb ik een nieuws bericht aangemaakt en verstuurt. Ik kwam niet terug in de detail pagina ik zag aleen het menu nog? En hij stuurt hem ook nie door naar twitter. Wat gaat er mis?
Wat doe ik verkeert?
Daarna heb ik een Gebruikergedefinieerde tags aangemaakt met de laatste code van arnoud ik moest wel de <?php en ?> weg halen anders pakte hij niet de rest is presies zo als er staat.
Deze heb ik gekoppelt in Gebeurtenissen Beheer aan NewsArticleAdded toegevoegt.
Vervolgens heb ik een nieuws bericht aangemaakt en verstuurt. Ik kwam niet terug in de detail pagina ik zag aleen het menu nog? En hij stuurt hem ook nie door naar twitter. Wat gaat er mis?
Wat doe ik verkeert?
Re: Nieuws koppelen aan Twitter via UDT en Events [update]
Heb je pretty url's op jouw site aanstaan?
Ter test kun je deze udt aan de update van een News artikel koppelen; dan hoef je niet steeds een nieuw artikel aan te maken
Grtz., Gregor
Ter test kun je deze udt aan de update van een News artikel koppelen; dan hoef je niet steeds een nieuw artikel aan te maken

Grtz., Gregor