Esempio n. 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);
        }
        IEnumerable <DirectMessageEvents> HandleDirectMessage(string dmJson)
        {
            DirectMessageEventsValue dmEvent = JsonConvert.DeserializeObject <DirectMessageEventsValue>(dmJson);

            return(new List <DirectMessageEvents>
            {
                new DirectMessageEvents
                {
                    Type = Type,
                    Count = Count,
                    Cursor = Cursor,
                    ID = ID,
                    Value = dmEvent
                }
            });
        }