Example #1
0
        /// <summary>
        /// Set typing indicators or send read receipts using the Send API, to let users know you are processing their request.
        /// Typing indicators are automatically turned off after 20 seconds
        /// </summary>
        /// <param name="userId">User identification</param>
        /// <param name="action"></param>
        /// <returns></returns>
        public async Task <Result> SendActionAsync(string userId, SenderAction action)
        {
            var result = new Result();

            try
            {
                var returnValue = (JObject) await PostTaskAsync("me/messages", new
                {
                    recipient = new
                    {
                        id = userId
                    },

                    sender_action = action.GetJsonPropertyName()
                });

                result.Error = CreateResultError(returnValue);
                if (result.Error == null)
                {
                    result.Message = returnValue.Value <string>("recipient_id");
                    result.Success = true;
                }
            }
            catch (Exception ex)
            {
                HandleException(ex, result);
            }

            return(result);
        }
Example #2
0
        /// <summary>
        /// Sends a sender action to a recipient via Facebook Messenger.
        /// </summary>
        /// <param name="recipient">The recipient in form of a valid <see cref="Recipient"/> subclass.</param>
        /// <param name="action">The sender action.</param>
        /// <returns></returns>
        public async Task SendAction(Recipient recipient, SenderAction action)
        {
            var message = new SendRequestContainerEntity
            {
                Recipient = recipient.ToEntity()
            };

            switch (action)
            {
            case SenderAction.TypingOn:
                message.SenderAction = "typing_on";
                break;

            case SenderAction.TypingOff:
                message.SenderAction = "typing_off";
                break;

            case SenderAction.MarkAsSeen:
                message.SenderAction = "mark_seen";
                break;

            default:
                throw new SenderActionNotSupportedException(action);
            }

            var result = await _client.Post <SendMessageResponse, MessengerErrorWrapperEntity>(ApiUri.ToString(), message);

            if (result.Error != null)
            {
                throw new MessengerRequestFailedException("text", ApiUri.GetLeftPart(UriPartial.Path), result.HttpCode, result.Error.MessengerError.Code,
                                                          result.Error.MessengerError.Message, result.Error.MessengerError.TraceId, result.Error.MessengerError.SubCode);
            }
        }
        public async Task SendSenderActionAsync(long userId, string senderAction)
        {
            var senderActionObj = new SenderAction {
                ActionType = senderAction
            };

            await SendApiMessagesParametersAsync(GenerateResponseModel(userId, null, senderActionObj));
        }
Example #4
0
 public async Task <SendApiResponse> SendActionAsync(string recipientID, SenderAction action)
 {
     return(await SendAsync(JObject.FromObject(new SenderActionContainer
     {
         Recipient = new Identifier {
             ID = recipientID
         },
         SenderAction = action
     }, new JsonSerializer {
         NullValueHandling = NullValueHandling.Ignore
     })));
 }
Example #5
0
        public void TypingOffTest()
        {
            var action = new SenderAction(SenderActionEnum.typing_off);

            action.Recipient.Id = "USER_ID";
            var str = JsonConvert.SerializeObject(action, new JsonSerializerSettings {
                NullValueHandling = NullValueHandling.Ignore,
            });
            var expected = "{\"sender_action\":\"typing_off\",\"recipient\":{\"id\":\"USER_ID\"}}";

            Assert.AreEqual(str, expected);
        }
 public Task SendActionAsync(string recipientId, SenderAction action)
 {
     return(MakeApiCall(
                new SendMessageRequest
     {
         Recipient = new Recipient
         {
             Id = recipientId
         },
         SenderAction = action
     }
                ));
 }
Example #7
0
 public RequestModel(long id, Message message, SenderAction senderAction)
 {
     Recipient    = new Recipient(id);
     Message      = message;
     SenderAction = senderAction?.ActionType;
 }
Example #8
0
 public SenderActionNotSupportedException(SenderAction action)
     : base($"Invalid sender action: {action.ToString()}")
 {
 }
        private RequestModel GenerateResponseModel(long id, Message message = null, SenderAction senderAction = null)
        {
            var responseModel = new RequestModel(id, message, senderAction);

            return(responseModel);
        }