Added image feature for Twitter notification.

This commit is contained in:
Christian P. MOMON 2018-05-19 04:25:52 +02:00
parent 64dbe8bcf3
commit 67891cd40f
7 changed files with 83 additions and 36 deletions

View File

@ -36,6 +36,7 @@ irc.channel=#april-test
notify.at15.cron=0 45 11 ? * * notify.at15.cron=0 45 11 ? * *
notify.at15.irc=Revue hebdomadaire dans 15 minutes notify.at15.irc=Revue hebdomadaire dans 15 minutes
notify.at15.twitter=Revue hebdomadaire !april dans 15 minutes sur irc://irc.freenode.org/april ou http://apr1.org/8l notify.at15.twitter=Revue hebdomadaire !april dans 15 minutes sur irc://irc.freenode.org/april ou http://apr1.org/8l
notify.at5.image=/srv/foo/photo-pizza.jpg
#notify.at30.cron=0 30 11 ? * * #notify.at30.cron=0 30 11 ? * *
#notify.at30.irc=Revue hebdomadaire dans 30 minutes #notify.at30.irc=Revue hebdomadaire dans 30 minutes

View File

@ -93,7 +93,18 @@ public class HebdobotConfigFile extends Properties
String notifyIrcMessage = getNotifyIrcMessage(notifyName); String notifyIrcMessage = getNotifyIrcMessage(notifyName);
String notifyTwitterMessage = getNotifyTwitterMessage(notifyName); String notifyTwitterMessage = getNotifyTwitterMessage(notifyName);
CronValue cronValue = new CronValue(notifyName, notifyCron, notifyIrcMessage, notifyTwitterMessage); String notifyImageFileName = getNotifyImage(notifyName);
File notifyImageFile;
if (notifyImageFileName == null)
{
notifyImageFile = null;
}
else
{
notifyImageFile = new File(notifyImageFileName);
}
CronValue cronValue = new CronValue(notifyName, notifyCron, notifyIrcMessage, notifyTwitterMessage, notifyImageFile);
result.add(cronValue); result.add(cronValue);
} }
@ -211,6 +222,23 @@ public class HebdobotConfigFile extends Properties
return result; return result;
} }
/**
* Gets the notify image file.
*
* @param notifyName
* the notify name
* @return the notify image file
*/
public String getNotifyImage(final String notifyName)
{
String result;
result = getProperty("notify." + notifyName + ".image");
//
return result;
}
/** /**
* Gets the notify irc message. * Gets the notify irc message.
* *

View File

@ -103,7 +103,7 @@ public class CronListener implements TriggerListener
} }
else else
{ {
this.bot.notifyTwitter(this.cron.getTwitterMessage()); this.bot.notifyTwitter(this.cron.getTwitterMessage(), this.cron.getImageFile());
} }
} }

View File

@ -18,6 +18,8 @@
*/ */
package org.april.hebdobot.cron; package org.april.hebdobot.cron;
import java.io.File;
/** /**
* The Class CronValue. * The Class CronValue.
*/ */
@ -27,6 +29,7 @@ public class CronValue
private String cron; private String cron;
private String ircMessage; private String ircMessage;
private String twitterMessage; private String twitterMessage;
private File imageFile;
/** /**
* Instantiates a new cron value. * Instantiates a new cron value.
@ -40,12 +43,13 @@ public class CronValue
* @param twitterMessage * @param twitterMessage
* the twitter message * the twitter message
*/ */
public CronValue(final String name, final String cron, final String ircMessage, final String twitterMessage) public CronValue(final String name, final String cron, final String ircMessage, final String twitterMessage, final File imageFile)
{ {
this.name = name; this.name = name;
this.cron = cron; this.cron = cron;
this.ircMessage = ircMessage; this.ircMessage = ircMessage;
this.twitterMessage = twitterMessage; this.twitterMessage = twitterMessage;
this.imageFile = imageFile;
} }
public String getCron() public String getCron()
@ -53,6 +57,11 @@ public class CronValue
return this.cron; return this.cron;
} }
public File getImageFile()
{
return this.imageFile;
}
public String getIrcMessage() public String getIrcMessage()
{ {
return this.ircMessage; return this.ircMessage;
@ -73,6 +82,11 @@ public class CronValue
this.cron = value; this.cron = value;
} }
public void setImageFile(final File imageFile)
{
this.imageFile = imageFile;
}
public void setIrcMessage(final String ircMessage) public void setIrcMessage(final String ircMessage)
{ {
this.ircMessage = ircMessage; this.ircMessage = ircMessage;

View File

@ -226,6 +226,19 @@ public class Hebdobot extends PircBot
* the message * the message
*/ */
public void notifyTwitter(final String message) public void notifyTwitter(final String message)
{
notifyTwitter(message, null);
}
/**
* Notify twitter.
*
* @param message
* the message
* @param imageFile
* the image file
*/
public void notifyTwitter(final String message, final File imageFile)
{ {
if ((this.twitterSettings.isValid()) && (StringUtils.isNotBlank(message))) if ((this.twitterSettings.isValid()) && (StringUtils.isNotBlank(message)))
{ {
@ -233,7 +246,8 @@ public class Hebdobot extends PircBot
{ {
TwitterClient twitter = new TwitterClient(this.twitterSettings.getConsumerKey(), this.twitterSettings.getConsumerSecret(), TwitterClient twitter = new TwitterClient(this.twitterSettings.getConsumerKey(), this.twitterSettings.getConsumerSecret(),
this.twitterSettings.getAccessToken(), this.twitterSettings.getAccessTokenSecret()); this.twitterSettings.getAccessToken(), this.twitterSettings.getAccessTokenSecret());
twitter.tweet(message);
twitter.tweet(message, imageFile);
} }
catch (TwitterException exception) catch (TwitterException exception)
{ {
@ -605,6 +619,8 @@ public class Hebdobot extends PircBot
{ {
logger.info("!stats caught."); logger.info("!stats caught.");
notifyTwitter("yooooooooooooo", null);
// Display statistics. This feature has to not break // Display statistics. This feature has to not break
// Hebdobot. // Hebdobot.
try try

View File

@ -106,14 +106,12 @@ public class ReviewStatsReporter
IntegerStat stat = board.get(currentDuration); IntegerStat stat = board.get(currentDuration);
int total = board.getCountSum(); int total = board.getCountSum();
/*
logger.debug("1 {}", stat.getValue()); logger.debug("1 {}", stat.getValue());
logger.debug("2 {}", board.getPositionOf(stat.getValue())); logger.debug("2 {}", board.getPositionOf(stat.getValue()));
logger.debug("3 {}", board.getMinValue()); logger.debug("3 {}", board.getMinValue());
logger.debug("4 {}", board.getAverage()); logger.debug("4 {}", board.getAverage());
logger.debug("5 {}", board.getMaxValue()); logger.debug("5 {}", board.getMaxValue());
logger.debug("6 {}", stat.getCount()); logger.debug("6 {}", stat.getCount());
*/
result = String.format( result = String.format(
"Statistiques sur la durée de la revue (%d mn) : position %d (min=%d mn,moy=%.1f mn,max=%d mn), fréquence %d/%d (%.0f %%)", "Statistiques sur la durée de la revue (%d mn) : position %d (min=%d mn,moy=%.1f mn,max=%d mn), fréquence %d/%d (%.0f %%)",

View File

@ -25,10 +25,10 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import twitter4j.Status; import twitter4j.Status;
import twitter4j.StatusUpdate;
import twitter4j.Twitter; import twitter4j.Twitter;
import twitter4j.TwitterException; import twitter4j.TwitterException;
import twitter4j.TwitterFactory; import twitter4j.TwitterFactory;
import twitter4j.UploadedMedia;
import twitter4j.conf.ConfigurationBuilder; import twitter4j.conf.ConfigurationBuilder;
/** /**
@ -94,31 +94,24 @@ public class TwitterClient
{ {
Status result; Status result;
if (StringUtils.isBlank(message)) result = tweet(message, null);
{
logger.info("Empty message => tweet aborted.");
result = null;
}
else
{
ConfigurationBuilder config = new ConfigurationBuilder();
config.setDebugEnabled(true);
config.setOAuthConsumerKey(this.consumerKey);
config.setOAuthConsumerSecret(this.consumerSecret);
config.setOAuthAccessToken(this.accessToken);
config.setOAuthAccessTokenSecret(this.accessTokenSecret);
Twitter twitter = new TwitterFactory(config.build()).getInstance();
result = twitter.updateStatus(message);
logger.info("Tweet result [" + result.getText() + "].");
}
// //
return result; return result;
} }
public Status tweet(final String message, final File image) throws TwitterException /**
* Tweet.
*
* @param message
* the message
* @param image
* the image
* @return the status
* @throws TwitterException
* the twitter exception
*/
public Status tweet(final String message, final File imageFile) throws TwitterException
{ {
Status result; Status result;
@ -137,18 +130,15 @@ public class TwitterClient
config.setOAuthAccessTokenSecret(this.accessTokenSecret); config.setOAuthAccessTokenSecret(this.accessTokenSecret);
Twitter twitter = new TwitterFactory(config.build()).getInstance(); Twitter twitter = new TwitterFactory(config.build()).getInstance();
StatusUpdate status = new StatusUpdate(message);
result = twitter.updateStatus("Test 1"); if ((imageFile != null) && (imageFile.exists()))
UploadedMedia um = twitter.uploadMedia(new File("/home/cpm/C/Hebdobot/TestConf/revue-hebdomadaire.png")); {
result = twitter.updateStatus("Test 2"); status.setMedia(imageFile);
result = twitter.updateStatus("Test 3"); }
result = twitter.updateStatus(status);
logger.info("Tweet result [" + result.getText() + "]."); logger.info("Tweet result [" + result.getText() + "].");
} }
// if ((imageFile != null) && (imageFile.exists()))
// {
// twitter
// }
// //
return result; return result;