Professional Applications Programmers/Consultants برمجة واستشارات تطبيقات الإنترنت
Skip Navigation LinksHome » Code Library » Twitter

Public general use code classes and xml files that we've compiled and used over the years:

Twitter API support class.

   1:  using System;
   2:  using System.Collections;
   3:  using System.Collections.Generic;
   4:  using System.ComponentModel;
   5:  using System.Data;
   6:  using System.Drawing;
   7:  using System.Text;
   8:  using System.Configuration;
   9:  using TweetSharp;
  10:  using Hammock;
  11:   
  12:  namespace Ia.Cl.Model
  13:  {
  14:      ////////////////////////////////////////////////////////////////////////////
  15:   
  16:      /// <summary publish="true">
  17:      /// Twitter API support class.
  18:      /// </summary>
  19:      /// <remarks> 
  20:      /// Copyright © 2001-2015 Jasem Y. Al-Shamlan (info@ia.com.kw), Internet Applications - Kuwait. All Rights Reserved.
  21:      ///
  22:      /// This library is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by
  23:      /// the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
  24:      ///
  25:      /// This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
  26:      /// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  27:      /// 
  28:      /// You should have received a copy of the GNU General Public License along with this library. If not, see http://www.gnu.org/licenses.
  29:      /// 
  30:      /// Copyright notice: This notice may not be removed or altered from any source distribution.
  31:      /// </remarks> 
  32:      public class Twitter
  33:      {
  34:          private string consumerKey, consumerSecret, accessToken, accessTokenSecret;
  35:          private TwitterService service;
  36:   
  37:          ////////////////////////////////////////////////////////////////////////////
  38:   
  39:          /// <summary>
  40:          ///
  41:          /// </summary>
  42:          public Twitter()
  43:          {
  44:              /*
  45:               * <appSettings>
  46:               * <add key="twitterConsumerKey" value="*" />
  47:               * <add key="twitterConsumerSecret" value="*" />
  48:               * <add key="twitterAccessToken" value="*" />
  49:               * <add key="twitterAccessTokenSecret" value="*" />
  50:               */
  51:   
  52:              consumerKey = ConfigurationManager.AppSettings["twitterConsumerKey"].ToString();
  53:              consumerSecret = ConfigurationManager.AppSettings["twitterConsumerSecret"].ToString();
  54:              accessToken = ConfigurationManager.AppSettings["twitterAccessToken"].ToString();
  55:              accessTokenSecret = ConfigurationManager.AppSettings["twitterAccessTokenSecret"].ToString();
  56:   
  57:              Initialize();
  58:          }
  59:   
  60:          ////////////////////////////////////////////////////////////////////////////
  61:   
  62:          /// <summary>
  63:          ///
  64:          /// </summary>
  65:          public Twitter(string _consumerKey, string _consumerSecret, string _accessToken, string _accessTokenSecret)
  66:          {
  67:              consumerKey = _consumerKey;
  68:              consumerSecret = _consumerSecret;
  69:              accessToken = _accessToken;
  70:              accessTokenSecret = _accessTokenSecret;
  71:   
  72:              Initialize();
  73:          }
  74:   
  75:          ////////////////////////////////////////////////////////////////////////////
  76:   
  77:          /// <summary>
  78:          ///
  79:          /// </summary>
  80:          private void Initialize()
  81:          {
  82:              // In v1.1, all API calls require authentication
  83:              service = new TwitterService(consumerKey, consumerSecret);
  84:              service.AuthenticateWith(accessToken, accessTokenSecret);
  85:          }
  86:   
  87:          ////////////////////////////////////////////////////////////////////////////
  88:   
  89:          /// <summary>
  90:          ///
  91:          /// </summary>
  92:          public List<TwitterStatus> TweetListAboveId(long aboveTweetId)
  93:          {
  94:              List<TwitterStatus> tweetList;
  95:              IEnumerable<TwitterStatus> iEnumerableTweetList;
  96:   
  97:              tweetList = new List<TwitterStatus>();
  98:              iEnumerableTweetList = service.ListTweetsOnHomeTimeline(new ListTweetsOnHomeTimelineOptions());
  99:   
 100:              foreach (TwitterStatus tweet in iEnumerableTweetList)
 101:              {
 102:                  if (tweet.Id > aboveTweetId) tweetList.Add(tweet);
 103:              }
 104:   
 105:              return tweetList;
 106:          }
 107:   
 108:          ////////////////////////////////////////////////////////////////////////////
 109:   
 110:          /// <summary>
 111:          ///
 112:          /// </summary>
 113:          public List<TwitterDirectMessage> DirectMessagesReceivedListAboveId(long aboveDmId)
 114:          {
 115:              List<TwitterDirectMessage> dmList;
 116:              IEnumerable<TwitterDirectMessage> iEnumerableDmList;
 117:   
 118:              dmList = new List<TwitterDirectMessage>();
 119:              iEnumerableDmList = service.ListDirectMessagesReceived(new ListDirectMessagesReceivedOptions());
 120:   
 121:              foreach (TwitterDirectMessage dm in iEnumerableDmList)
 122:              {
 123:                  if (dm.Id > aboveDmId) dmList.Add(dm);
 124:              }
 125:   
 126:              return dmList;
 127:          }
 128:   
 129:          ////////////////////////////////////////////////////////////////////////////
 130:   
 131:          /// <summary>
 132:          ///
 133:          /// </summary>
 134:          public List<TwitterStatus> TweetList()
 135:          {
 136:              // 
 137:              List<TwitterStatus> tweetList;
 138:              IEnumerable<TwitterStatus> iEnumerableTweetList;
 139:   
 140:              tweetList = new List<TwitterStatus>();
 141:              iEnumerableTweetList = service.ListTweetsOnHomeTimeline(new ListTweetsOnHomeTimelineOptions());
 142:   
 143:              foreach (TwitterStatus tweet in tweetList)
 144:              {
 145:                  tweetList.Add(tweet);
 146:              }
 147:   
 148:              return tweetList;
 149:          }
 150:   
 151:          ////////////////////////////////////////////////////////////////////////////
 152:   
 153:          /// <summary>
 154:          ///
 155:          /// </summary>
 156:          public List<TwitterDirectMessage> DirectMessageList()
 157:          {
 158:              // 
 159:              List<TwitterDirectMessage> dmList;
 160:              IEnumerable<TwitterDirectMessage> iEnumerableTweetList;
 161:   
 162:              dmList = new List<TwitterDirectMessage>();
 163:              iEnumerableTweetList = service.ListDirectMessagesReceived(new ListDirectMessagesReceivedOptions());
 164:   
 165:              foreach (TwitterDirectMessage dm in dmList)
 166:              {
 167:                  dmList.Add(dm);
 168:              }
 169:   
 170:              return dmList;
 171:          }
 172:   
 173:          ////////////////////////////////////////////////////////////////////////////
 174:   
 175:          /// <summary>
 176:          ///
 177:          /// </summary>
 178:          public TwitterStatus ReadLastTweet()
 179:          {
 180:              // send tweet
 181:              TwitterStatus ts;
 182:              IEnumerable<TwitterStatus> tweetList;
 183:   
 184:              ts = null;
 185:              tweetList = service.ListTweetsOnHomeTimeline(new ListTweetsOnHomeTimelineOptions());
 186:   
 187:              foreach (TwitterStatus tweet in tweetList)
 188:              {
 189:                  ts = tweet;
 190:                  break; // to read only the first;
 191:              }
 192:   
 193:              return ts;
 194:          }
 195:   
 196:          ////////////////////////////////////////////////////////////////////////////
 197:   
 198:          /// <summary>
 199:          /// Delete Tweet (this function is not working properly)
 200:          /// <param name="tweetId">Tweet Id to delete</param>
 201:          /// <see cref="http://stackoverflow.com/questions/4810076/tweetsharp-remove-undo-retweet"/>
 202:          /// </summary>
 203:          public TwitterStatus DeleteTweet(long tweetId)
 204:          {
 205:              // delete tweet
 206:              TwitterStatus ts;
 207:   
 208:              ts = service.DeleteTweet(new DeleteTweetOptions() { Id = tweetId });
 209:   
 210:              return ts;
 211:          }
 212:   
 213:          ////////////////////////////////////////////////////////////////////////////
 214:   
 215:          /// <summary>
 216:          /// Delete DM
 217:          /// </summary>
 218:          public TwitterDirectMessage DeleteDirectMessage(long dmId)
 219:          {
 220:              // delete tweet
 221:              TwitterDirectMessage dm;
 222:   
 223:              dm = service.DeleteDirectMessage(new DeleteDirectMessageOptions() { Id = dmId });
 224:   
 225:              return dm;
 226:          }
 227:   
 228:          ////////////////////////////////////////////////////////////////////////////
 229:   
 230:          /// <summary>
 231:          ///
 232:          /// </summary>
 233:          public void SendTweet(string status)
 234:          {
 235:              // send tweet
 236:   
 237:              SendTweetOptions sto;
 238:   
 239:              sto = new SendTweetOptions();
 240:   
 241:              sto.Status = status; // "Testing (2)...";
 242:   
 243:              service.SendTweet(sto);
 244:          }
 245:   
 246:          ////////////////////////////////////////////////////////////////////////////
 247:   
 248:          /// <summary>
 249:          ///
 250:          /// </summary>
 251:          public void SendDirectMessage(string screenName, string text)
 252:          {
 253:              // direct message
 254:   
 255:              SendDirectMessageOptions sdmo;
 256:   
 257:              sdmo = new SendDirectMessageOptions();
 258:   
 259:              sdmo.ScreenName = screenName; // "@abcd";
 260:              sdmo.Text = text; // "Testing (2)...";
 261:   
 262:              service.SendDirectMessage(sdmo);
 263:          }
 264:   
 265:          ////////////////////////////////////////////////////////////////////////////
 266:          ////////////////////////////////////////////////////////////////////////////
 267:      }
 268:  }