Ejemplo n.º 1
0
  /**
   * Sends a direct message via Twitter
   *
   * @param recipientId the receiver of this direct message
   * @param messageTxt the direct message to send
   * @return <code>true</code>, if sending the direct message has been successful and <code>false
   *     </code> in all other cases.
   */
  @ActionDoc(
      text = "Sends a direct message via Twitter",
      returns =
          "<code>true</code>, if sending the direct message has been successful and <code>false</code> in all other cases.")
  public static boolean sendDirectMessage(
      @ParamDoc(name = "recipientId", text = "the receiver of this direct message")
          String recipientId,
      @ParamDoc(name = "messageTxt", text = "the direct message to send") String messageTxt) {
    if (!isEnabled) {
      logger.debug("Twitter client is disabled > execution aborted!");
      return false;
    }

    try {
      // abbreviate the Tweet to meet the 140 character limit ...
      messageTxt = StringUtils.abbreviate(messageTxt, CHARACTER_LIMIT);
      // send the direct message
      DirectMessage message = client.sendDirectMessage(recipientId, messageTxt);
      logger.debug(
          "Successfully sent direct message '{}' to @",
          message.getText(),
          message.getRecipientScreenName());
      return true;
    } catch (TwitterException e) {
      logger.error(
          "Failed to send Tweet '" + messageTxt + "' because of: " + e.getLocalizedMessage());
      return false;
    }
  }
Ejemplo n.º 2
0
  @Override
  public void onDirectMessage(DirectMessage directMessage) {
    Debug.logln(
        "TwitterServer.onDirectMessage sender: "
            + directMessage.getSenderScreenName()
            + " recipient: "
            + directMessage.getRecipientScreenName()
            + " text: "
            + directMessage.getText());

    if (directMessage.getSenderId() == this.ownerID) {
      return; // outgoing message
    }

    // incoming message
    String text = directMessage.getText();
    String sender = directMessage.getSenderScreenName();
    String recipient = directMessage.getRecipientScreenName();

    // Detect trackback
    Trackback trackback = null;
    try {
      trackback = TrackbackStore.getInstance().loadByIncomingText(Channel.TWITTER, sender, text);
    } catch (Exception e) {
      LogEntryStore.log(e);
    }
    if (trackback == null) {
      // !$! Send back error message?
      return;
    }

    // Dispatch event
    TwitterMessage msg = new TwitterMessage();
    msg.setDestination(recipient);
    msg.setSender(sender);
    msg.write(TrackbackStore.getInstance().cleanIncomingText(text));

    this.listener.onTwitterReceived(msg, trackback.getExternalID());
  }
Ejemplo n.º 3
0
 public void testDirectMessagesAsJSON() throws Exception {
   HttpClientImpl http = new HttpClientImpl();
   List<DirectMessage> directMessages =
       DirectMessageJSONImpl.createDirectMessageList(
           http.get("http://twitter4j.org/en/testcases/direct_messages.json"), conf);
   DirectMessage dm = directMessages.get(0);
   Assert.assertEquals(new java.util.Date(1248177356000l), dm.getCreatedAt());
   Assert.assertEquals(6358482, dm.getRecipient().getId());
   Assert.assertEquals(246928323, dm.getId());
   Assert.assertEquals(6358482, dm.getRecipientId());
   Assert.assertEquals("twit4j", dm.getRecipientScreenName());
   Assert.assertEquals(6377362, dm.getSender().getId());
   Assert.assertEquals(6377362, dm.getSenderId());
   Assert.assertEquals("twit4j2", dm.getSenderScreenName());
   Assert.assertEquals("Tue Jul 21 20:55:39 KST 2009:directmessage test", dm.getText());
   assertDeserializedFormIsEqual(directMessages);
 }