Пример #1
0
        /// <summary>
        /// Sends a new direct message to specified user.
        /// </summary>
        /// <param name="recipientID">ID of user to send to.</param>
        /// <param name="text">Direct message contents.</param>
        /// <param name="attachment">Optional attachment from overloads that support it.</param>
        /// <param name="quickReply">Quick reply from overloads that support it.</param>
        /// <param name="callToActions">List of Call to Action, which creates buttons in the message.</param>
        /// <param name="cancelToken">Async cancellation token.</param>
        /// <returns>Direct message events data.</returns>
        public async Task <DirectMessageEvents?> NewDirectMessageEventAsync(ulong recipientID, string text, Attachment?attachment, QuickReply?quickReply, IEnumerable <CallToAction>?callToActions, CancellationToken cancelToken = default)
        {
            if (recipientID == default)
            {
                throw new ArgumentException($"{nameof(recipientID)} must be set.", nameof(recipientID));
            }
            if (string.IsNullOrWhiteSpace(text))
            {
                throw new ArgumentException($"{nameof(text)} is a required parameter.", nameof(text));
            }

            var newUrl = BaseUrl + "direct_messages/events/new.json";

            var dmEventParams = new DirectMessageEventsValue
            {
                DMEvent = new DMEvent
                {
                    MessageCreate = new DirectMessageCreate
                    {
                        MessageData = new DirectMessageData
                        {
                            Text          = text,
                            Attachment    = attachment,
                            QuickReply    = quickReply,
                            CallToActions = callToActions
                        },
                        Target = new DirectMessageTarget
                        {
                            RecipientID = recipientID.ToString()
                        }
                    },
                    Type = "message_create"
                },
                Apps = JsonDocument.Parse("null").RootElement
            };

            RawResult =
                await TwitterExecutor.SendJsonToTwitterAsync(
                    HttpMethod.Post.ToString(),
                    newUrl,
                    new Dictionary <string, string>(),
                    dmEventParams,
                    cancelToken)
                .ConfigureAwait(false);

            var reqProc = new DirectMessageEventsRequestProcessor <DirectMessageEvents>();
            DirectMessageEvents?dmEvents = reqProc.ProcessActionResult(RawResult, DirectMessageEventsType.Show);

            return(dmEvents);
        }
Пример #2
0
        protected internal IRequestProcessor <T> CreateRequestProcessor <T>(string requestType)
            where T : class
        {
            string baseUrl = BaseUrl;
            IRequestProcessor <T> req;

            switch (requestType)
            {
            case nameof(Account):
                req = new AccountRequestProcessor <T>();
                break;

            case nameof(AccountActivity):
                req = new AccountActivityRequestProcessor <T>();
                break;

            case nameof(Blocks):
                req = new BlocksRequestProcessor <T>();
                break;

            case nameof(ControlStream):
                req = new ControlStreamRequestProcessor <T>
                {
                    SiteStreamUrl = SiteStreamUrl
                };
                break;

            case nameof(DirectMessage):
                req = new DirectMessageRequestProcessor <T>();
                break;

            case nameof(DirectMessageEvents):
                req = new DirectMessageEventsRequestProcessor <T>();
                break;

            case nameof(Favorites):
                req = new FavoritesRequestProcessor <T>();
                break;

            case nameof(Friendship):
                req = new FriendshipRequestProcessor <T>();
                break;

            case nameof(Geo):
                req = new GeoRequestProcessor <T>();
                break;

            case nameof(Help):
                req = new HelpRequestProcessor <T>();
                break;

            case nameof(List):
                req = new ListRequestProcessor <T>();
                break;

            case nameof(Media):
                req = new MediaRequestProcessor <T>
                {
                    UploadUrl = UploadUrl
                };
                break;

            case nameof(Mute):
                req = new MuteRequestProcessor <T>();
                break;

            case nameof(Raw):
                req = new RawRequestProcessor <T>();
                break;

            case nameof(SavedSearch):
                req = new SavedSearchRequestProcessor <T>();
                break;

            case nameof(Search):
                req = new SearchRequestProcessor <T>();
                break;

            case nameof(Status):
                req = new StatusRequestProcessor <T>();
                break;

            case nameof(Streaming):
                baseUrl = StreamingUrl;
                req     = new StreamingRequestProcessor <T>
                {
                    UserStreamUrl   = UserStreamUrl,
                    SiteStreamUrl   = SiteStreamUrl,
                    TwitterExecutor = TwitterExecutor
                };
                break;

            case nameof(Trend):
                req = new TrendRequestProcessor <T>();
                break;

            case nameof(User):
                req = new UserRequestProcessor <T>();
                break;

            case nameof(Vine):
                req = new VineRequestProcessor <T>
                {
                    VineUrl = VineUrl
                };
                break;

            case nameof(WelcomeMessage):
                req = new WelcomeMessageRequestProcessor <T>();
                break;

            default:
                throw new ArgumentException($"Type, {requestType} isn't a supported LINQ to Twitter entity.", nameof(requestType));
            }

            if (baseUrl != null)
            {
                req.BaseUrl = baseUrl;
            }

            return(req);
        }