Пример #1
0
        /// <inheritdoc />
        /// <summary>
        /// Sends a text message
        /// </summary>
        /// <param name="chat">Chat where the message will be sent</param>
        /// <param name="message">Message to send</param>
        /// <param name="parseMode">Message parse mode</param>
        /// <returns>Async task</returns>
        public Task SendTextMessageAsync(BotChat chat, string message, MessageParseMode parseMode = MessageParseMode.Default)
        {
            var taskSource = new TaskCompletionSource <bool>(TaskCreationOptions.RunContinuationsAsynchronously);

            _client.SendMessage(received => taskSource.TrySetResult(true), chat.Id, message);
            return(taskSource.Task);
        }
Пример #2
0
 public async Task SendTypingAsync(BotChat chat)
 {
     if (!_active)
     {
         return;
     }
     await _bot.SendChatActionAsync(chat.Id.ParseTo <long>(-1), TBot.Types.Enums.ChatAction.Typing).ConfigureAwait(false);
 }
Пример #3
0
 public async Task SendPhotoMessageAsync(BotChat chat, Stream fileStream, string caption = null)
 {
     if (!_active)
     {
         return;
     }
     Ensure.ArgumentNotNull(fileStream, "The fileStream can't be null");
     await _bot.SendPhotoAsync(chat.Id.ParseTo <long>(-1), fileStream, caption).ConfigureAwait(false);
 }
Пример #4
0
        public void ChatMessage(string channel, string text)
        {
            BotChat chat = m_ChatCallback;

            if (chat != null)
            {
                chat(channel, text);
            }
        }
Пример #5
0
 public async Task SendTextMessageAsync(BotChat chat, string message, MessageParseMode parseMode = MessageParseMode.Default)
 {
     if (!_active)
     {
         return;
     }
     await _bot.SendTextMessageAsync(chat.Id.ParseTo <long>(-1), message,
                                     disableWebPagePreview : DisableWebPagePreview,
                                     parseMode : (TBot.Types.Enums.ParseMode)parseMode).ConfigureAwait(false);
 }
Пример #6
0
 public async Task SendPhotoMessageAsync(BotChat chat, Stream fileStream, string caption = null)
 {
     if (!_active)
     {
         return;
     }
     Ensure.ArgumentNotNull(fileStream, "The fileStream can't be null");
     var fileToSend = new TBot.Types.FileToSend(caption ?? Core.Now.Ticks.ToString(), fileStream);
     await _bot.SendPhotoAsync(chat.Id.ParseTo <long>(-1), fileToSend, caption).ConfigureAwait(false);
 }
Пример #7
0
        public async Task SendPhotoMessageAsync(BotChat chat, string fileName, string caption = null)
        {
            if (!_active)
            {
                return;
            }
            if (!File.Exists(fileName))
            {
                throw new FileNotFoundException("The filename doesn't exist", fileName);
            }

            using (var ms = new MemoryStream())
            {
                using (var fs = File.Open(fileName, FileMode.Open, FileAccess.Read, FileShare.Read))
                    await fs.CopyToAsync(ms).ConfigureAwait(false);
                ms.Position = 0;
                await _bot.SendPhotoAsync(chat.Id.ParseTo <long>(-1), ms, caption).ConfigureAwait(false);
            }
        }
Пример #8
0
        public async Task Log(Activity activity, User u)
        {
            var     x    = activity;
            BotChat chat = new BotChat(x.From.Name == null ? "" : x.From.Name.ToString() + "(" +
                                       x.From.Id == null ? "" :  x.From.Id.ToString() + ")",
                                       x.Text == null ? "" : x.Text,
                                       x.ChannelId == null ? "" : x.ChannelId.ToString(),
                                       x.Timestamp == null ? DateTime.Now : x.Timestamp.Value,
                                       x.Conversation.Id);

            if (u.existingChatID != Guid.Empty && u.ConversationId != string.Empty && u.ConversationId == activity.Conversation.Id)
            {
                chat.existingChatID = u.existingChatID;
            }
            if (u.CRMContactId != Guid.Empty)
            {
                chat.regardingId = u.CRMContactId;
            }

            HttpClient cons = new HttpClient();

            cons.BaseAddress = new Uri("https://crmapikyliebot.azurewebsites.net/");
            cons.DefaultRequestHeaders.Accept.Clear();
            cons.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
            cons.Timeout = TimeSpan.FromMinutes(1);

            using (cons)
            {
                var content             = new StringContent(JsonConvert.SerializeObject(chat), Encoding.UTF8, "application/json");
                HttpResponseMessage res = await cons.PostAsync("CRM/CreateBotChat", content);

                if (res.IsSuccessStatusCode)
                {
                    Tuple <bool, Guid> result = await res.Content.ReadAsAsync <Tuple <bool, Guid> >();

                    if (u.existingChatID == Guid.Empty && result.Item1 && result.Item2 != Guid.Empty)
                    {
                        int ind = MessagesController.ConverastionalUserList.FindIndex(y => y.Id == activity.From.Id);
                        MessagesController.ConverastionalUserList[ind].existingChatID = result.Item2;
                    }
                }
            }
        }
Пример #9
0
        public static async Task Log(Activity activity)
        {
            var     x    = activity;
            BotChat chat = new BotChat(x.From.Name.ToString() + "(" + x.From.Id.ToString() + ")", x.Text, x.ChannelId.ToString(), x.Timestamp.Value);

            if (MessagesController.LocalUser.existingChatID != null)
            {
                chat.existingChatID = MessagesController.LocalUser.existingChatID;
            }
            if (MessagesController.LocalUser.CRMContactId != null)
            {
                chat.regardingId = MessagesController.LocalUser.CRMContactId;
            }

            HttpClient cons = new HttpClient();

            cons.BaseAddress = new Uri("");
            cons.DefaultRequestHeaders.Accept.Clear();
            cons.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));

            using (cons)
            {
                var content             = new StringContent(JsonConvert.SerializeObject(chat), Encoding.UTF8, "application/json");
                HttpResponseMessage res = await cons.PostAsync("CRM/CreateBotChat", content);

                if (res.IsSuccessStatusCode)
                {
                    Tuple <bool, Guid> result = await res.Content.ReadAsAsync <Tuple <bool, Guid> >();

                    if (MessagesController.LocalUser.existingChatID == Guid.Empty && result.Item1 && result.Item2 != Guid.Empty)
                    {
                        MessagesController.LocalUser.existingChatID = result.Item2;
                    }
                }
            }
        }
Пример #10
0
 /// <inheritdoc />
 /// <summary>
 /// Send the typing action to the chat
 /// </summary>
 /// <param name="chat">Chat where the typing action will be sent</param>
 /// <returns>Async task</returns>
 public Task SendTypingAsync(BotChat chat)
 {
     _client.SendTyping(chat.Id);
     return(Task.CompletedTask);
 }
Пример #11
0
 /// <inheritdoc />
 /// <summary>
 /// Sends a photo message
 /// </summary>
 /// <param name="chat">Chat where the message will be sent</param>
 /// <param name="fileStream">Photo stream</param>
 /// <param name="caption">Photo Caption</param>
 /// <returns>Async task</returns>
 public Task SendPhotoMessageAsync(BotChat chat, Stream fileStream, string caption = null)
 {
     return(Task.CompletedTask);
 }
Пример #12
0
 public void SetChatCallback(BotChat callback)
 {
     m_ChatCallback = callback;
 }