public ResponseAction PostTwitter(PostToTwitterViewModel message) { ResponseAction rsp = new ResponseAction(); rsp.Success = true; bool trysended = false; List <TwitterAccountPart> TwitterAccountSettings = Twitter_GetAccessToken(message.AccountList); ProviderConfigurationRecord pcr = _providerConfigurationService.Get("Twitter"); foreach (TwitterAccountPart Faccount in TwitterAccountSettings) { try { trysended = true; OAuthTokens accesstoken = new OAuthTokens() { AccessToken = Faccount.UserToken, AccessTokenSecret = Faccount.UserTokenSecret, ConsumerKey = pcr.ProviderIdKey, ConsumerSecret = pcr.ProviderSecret }; TwitterResponse <TwitterStatus> response; string realmessage = message.Message; if (!string.IsNullOrEmpty(message.Link)) { realmessage += " " + message.Link; } if (string.IsNullOrEmpty(message.Picture)) { response = TwitterStatus.Update(accesstoken, realmessage.Trim()); } else { var mediaPath = HostingEnvironment.IsHosted ? HostingEnvironment.MapPath("~/Media/") ?? "" : Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Media"); string physicalPath = mediaPath + _shellsettings.Name + "\\" + message.Picture; byte[] photo = System.IO.File.ReadAllBytes(physicalPath); response = TwitterStatus.UpdateWithMedia(accesstoken, realmessage.Trim(), photo, new StatusUpdateOptions() { UseSSL = true, APIBaseAddress = "http://api.twitter.com/1.1/" }); } if (response.Result != RequestResult.Success) { if (response.Content != null) { Logger.Error("response.Content:" + response.Content); } rsp.Success = false; if (response.ErrorMessage != null) { Logger.Error("response.ErrorMessage:" + response.ErrorMessage); _notifier.Add(NotifyType.Error, T("Can't post on twitter: {0} {1}", Faccount.DisplayAs, response.ErrorMessage)); } else { var serializer = new System.Web.Script.Serialization.JavaScriptSerializer(); var jsondict = serializer.Deserialize <Dictionary <string, object> >(response.Content); ArrayList errors = (ArrayList)jsondict["errors"]; foreach (System.Collections.Generic.Dictionary <string, object> error in errors) { string errormsg = ""; foreach (var errordict in error) { errormsg += " " + errordict.Key.ToString() + ":" + errordict.Value.ToString(); } _notifier.Add(NotifyType.Error, T("Can't post on twitter: {0} {1}", Faccount.DisplayAs, errormsg)); } } } } catch (Exception ex) { Logger.Error("Twitter Posting Error Message::" + ex.Message); rsp.Success = false; rsp.Message = "Twitter Posting Error Message: " + ex.Message; _notifier.Add(NotifyType.Error, T("Twitter Posting {0} Error Message: {1}", Faccount.DisplayAs, ex.Message)); } } if (trysended && rsp.Success) { _notifier.Add(NotifyType.Information, T("Twitter posted")); } return(rsp); }
public TwitterPostHandler(ITokenizer tokenizer, IRepository <TwitterPostPartRecord> repository, ITwitterService TwitterService, IOrchardServices orchardServices, INotifier notifier) { _TwitterService = TwitterService; _orchardServices = orchardServices; _tokenizer = tokenizer; _notifier = notifier; T = NullLocalizer.Instance; Filters.Add(StorageFilter.For(repository)); // Filters.Add(new ActivatingFilter<TwitterPostPart>("CommunicationAdvertising")); OnUpdated <TwitterPostPart>((context, part) => { TwitterPostPartSettingVM setting = part.Settings.GetModel <TwitterPostPartSettingVM>(); var tokens = new Dictionary <string, object> { { "Content", part.ContentItem } }; if (!string.IsNullOrEmpty(setting.Description)) { part.TwitterDescription = _tokenizer.Replace(setting.Description, tokens); } var urlHelper = new UrlHelper(_orchardServices.WorkContext.HttpContext.Request.RequestContext); if (string.IsNullOrEmpty(setting.Image)) { part.TwitterPicture = ""; } else { string listid = _tokenizer.Replace(setting.Image, tokens); listid = listid.Replace("{", "").Replace("}", ""); Int32 idimage = 0; Int32.TryParse(listid.Split(',')[0], out idimage); if (idimage > 0) { var ContentImage = _orchardServices.ContentManager.Get(idimage, VersionOptions.Published); var pathdocument = Path.Combine(ContentImage.As <MediaPart>().FolderPath, ContentImage.As <MediaPart>().FileName); part.TwitterPicture = pathdocument; } else { part.TwitterPicture = ""; } } if (!string.IsNullOrEmpty(setting.Title)) { part.TwitterTitle = _tokenizer.Replace(setting.Title, tokens); } }); OnPublished <TwitterPostPart>((context, Twitterpart) => { try { PostToTwitterViewModel Fvm = new PostToTwitterViewModel(); Fvm.Message = Twitterpart.TwitterMessage; if (Twitterpart.ContentItem.ContentType == "CommunicationAdvertising") { ICommunicationService _communicationService; bool tryed = _orchardServices.WorkContext.TryResolve <ICommunicationService>(out _communicationService); if (tryed) { Fvm.Link = _communicationService.GetCampaignLink("Twitter", Twitterpart); } else { Fvm.Link = ""; } } else if (Twitterpart.TwitterCurrentLink) { var urlHelper = new UrlHelper(_orchardServices.WorkContext.HttpContext.Request.RequestContext); Fvm.Link = urlHelper.MakeAbsolute(urlHelper.ItemDisplayUrl(Twitterpart));// get current display link } Fvm.Picture = Twitterpart.TwitterPicture; Fvm.AccountList = Twitterpart.AccountList; if (Twitterpart.SendOnNextPublish && !Twitterpart.TwitterMessageSent) { if (Twitterpart.AccountList.Length == 0) { _notifier.Add(NotifyType.Warning, T("No Twitter account specified.")); } else { ResponseAction rsp = _TwitterService.PostTwitter(Fvm); if (rsp.Success) { Twitterpart.TwitterMessageSent = true; } } } } catch (Exception ex) { _notifier.Add(NotifyType.Error, T("Twitter error:" + ex.Message)); } }); }