예제 #1
0
        public void HandleUserMessage(ITelegramUserMessage message)
        {
            if (message.IsEmpty())
            {
                return;
            }

            ISessionManager sessionManager = ModulesManager.GetModulesManager().GetSessionManager();

            IChatSession chatSession = sessionManager.GetChatSession(message.ChatId);

            if (chatSession == null)
            {
                sessionManager.CreateChatSession(message.ChatId);
                chatSession = sessionManager.GetChatSession(message.ChatId);
            }

            sessionManager.UserSentMessage(message.ChatId, message.MessageId);
            if (string.Equals(message.Message, "/e") || string.Equals(message.Message, "/exit"))
            {
                sessionManager.KillChatSession(message.ChatId);
            }
            else
            {
                chatStateHandlerFactory.GetChatStateHandler(chatSession.State).HandleUserMessage(message, this);
            }
        }
 public LoginMessageHandler(
     ITelegramBotClient telegramBotClient,
     IChatSession chatSession)
 {
     _telegramBotClient = telegramBotClient;
     _chatSession       = chatSession;
 }
예제 #3
0
 public void BotSentMessage(long chatId, int messageId)
 {
     if (IsChatSessionExist(chatId))
     {
         IChatSession chatSession = currentChatSessions[chatId];
         chatSession.BotSentMessage(messageId);
     }
 }
 public RequestReplyMessageHandler(
     IUserChatHubSession userChatHubSession,
     IChatSession chatSession,
     ITelegramBotClient telegramBotClient)
 {
     _userChatHubSession = userChatHubSession;
     _chatSession        = chatSession;
     _telegramBotClient  = telegramBotClient;
 }
예제 #5
0
        public override async Task SendMessageAsync(IChatSession sess, IOutgoingMessage msg)
        {
            if (!(sess is ChatSession))
            {
                throw new Exception("Bad chat session type");
            }
            var s = sess as ChatSession;

            switch (msg.Type)
            {
            case MsgOutType.Text:
            {
                var m = new TextMessage
                {
                    Text     = msg.Text,
                    Receiver = s.InternalChatId
                };
                await _cli.SendTextMessageAsync(m);

                break;
            }

            case MsgOutType.Keyboard:
            {
                var keyboardMsg = new KeyboardMessage
                {
                    Text          = msg.Text,
                    Receiver      = s.InternalChatId,
                    Keyboard      = GetViberKeyboard(msg.Keyboard.Buttons.ToArray()),
                    MinApiVersion = 3
                };
                await _cli.SendKeyboardMessageAsync(keyboardMsg);

                break;
            }

            case MsgOutType.Photo:
                break;

            case MsgOutType.Wait:
            {
                var m = new TextMessage
                {
                    Text     = $"Нагадування встановлено",
                    Receiver = s.InternalChatId
                };
                await _cli.SendTextMessageAsync(m);

                break;
            }

            default:
                break;
            }
        }
예제 #6
0
        protected override bool ParseIncomingMessage(String json, out IChatSession s, out IIncomingMessage m)
        {
            s = null;
            m = null;
            CallbackData data = JsonConvert.DeserializeObject <CallbackData>(json);

            switch (data.Event)
            {
            case EventType.Delivered:
                return(false);

            case EventType.Seen:
                return(false);

            case EventType.Failed:
                return(false);

            case EventType.Subscribed:
            {
                m = new IncomingMessage();
                s = new ChatSession(data.User.Id);
                return(true);
            }

            case EventType.Unsubscribed:
            {
                s = new ChatSession(data.UserId);
                return(false);
            }

            case EventType.ConversationStarted:
            {
                return(false);
            }

            case EventType.Message:
            {
                m = new IncomingMessage(data);
                s = new ChatSession(data.Sender.Id);
                return(true);
            }

            case EventType.Webhook:
            {
                return(false);
            }

            case EventType.Action:
                return(false);

            default:
                return(false);
            }
        }
        public ChatHubUserMessage(
            string text,
            DateTime sentAt,
            IChatHubHistory history,
            Func <IServiceProvider> serviceProviderFactory)
            : base(text, sentAt, history)
        {
            var provider = serviceProviderFactory();

            _chatSession = provider.GetRequiredService <IChatSession>();
        }
예제 #8
0
 private void ClearSessions(object sender, ElapsedEventArgs e)
 {
     foreach (var s in currentChatSessions)
     {
         IChatSession chatSession = s.Value;
         if (chatSession.NeedKill())
         {
             KillChatSession(chatSession.ChatId);
         }
     }
 }
예제 #9
0
        public void GetChatSessionTest()
        {
            const int    chatSessionId = 12;
            IChatSession chatSession1  = sessionManager.GetChatSession(chatSessionId);

            sessionManager.CreateChatSession(chatSessionId);
            IChatSession chatSession2 = sessionManager.GetChatSession(chatSessionId);

            Assert.AreEqual(chatSession1, null);
            Assert.AreNotEqual(chatSession2, null);
        }
예제 #10
0
        public IEnumerable <IOutgoingMessage> ProcessIncomingMessage(IChatSession sess, IIncomingMessage msg)
        {
            var m = new IncomeMessage(sess.ChatId)
            {
                BotEngine = engine,
                BotKey    = key,
                Message   = msg
            };

            bus.Send(m);
            yield break;
        }
예제 #11
0
        public IChat StartChat(IBuddy buddy)
        {
            if (!IsLoggedIn)
            {
                throw new InvalidOperationException("Not logged in.");
            }

            IChatSession session = chatService.CreateSession(new SquiggleEndPoint(buddy.Id, ((Buddy)buddy).ChatEndPoint));
            var          chat    = new Chat(session, CurrentUser, new[] { buddy }, id => buddies[id], history);

            return(chat);
        }
예제 #12
0
        public ChatSession(IChatSession source)
        {
            var ba = source.ChatId.ToByteArray();
            var i1 = BitConverter.ToInt64(ba, 0);
            var i2 = BitConverter.ToInt64(ba, 8);

            if (i1 != i2)
            {
                throw new Exception("Bad ChatId format");
            }
            InternalChatId = i1;
        }
예제 #13
0
        public void ChangeChatState(long chatId, ChatSessionState newState)
        {
            ISessionManager sessionManager = ModulesManager.GetSessionManager();

            IChatSession chatSession = sessionManager.GetChatSession(chatId);

            if (chatSession != null)
            {
                chatSession.State = newState;
                chatStateHandlerFactory.GetChatStateHandler(chatSession.State).StartStateMessage(chatId);
            }
        }
예제 #14
0
        public IChatSession CreateSession(ISquiggleEndPoint endPoint)
        {
            IChatSession result = chatSessions.Find(s => !s.IsGroupSession && s.RemoteUsers.Contains(endPoint));

            if (result == null)
            {
                var session = CreateSession(Guid.NewGuid(), endPoint);
                session.Initialize(false);
                result = session;
            }
            return(result);
        }
예제 #15
0
        protected override bool ParseIncomingMessage(String json, out IChatSession s, out IIncomingMessage m)
        {
            s = null;
            m = null;
            Update update = JsonConvert.DeserializeObject <Update>(json);

            switch (update.Type)
            {
            case UpdateType.Unknown:
                return(false);

            case UpdateType.Message:
            {
                m = new IncomingMessage(update.Message);
                s = new ChatSession(update.Message.Chat.Id);
                return(true);
            }

            case UpdateType.InlineQuery:
                return(false);

            case UpdateType.ChosenInlineResult:
                return(false);

            case UpdateType.CallbackQuery:
            {
                var message = update.CallbackQuery.Message;
                message.Text = update.CallbackQuery.Data;
                m            = new IncomingMessage(message);
                s            = new ChatSession(message.Chat.Id);
                return(true);
            }

            case UpdateType.EditedMessage:
                return(false);

            case UpdateType.ChannelPost:
                return(false);

            case UpdateType.EditedChannelPost:
                return(false);

            case UpdateType.ShippingQuery:
                return(false);

            case UpdateType.PreCheckoutQuery:
                return(false);

            default:
                return(false);
            }
        }
예제 #16
0
        public LongPollingChat(
            IServiceScopeFactory serviceScopeFactory,
            ITelegramBotClient client,
            Func <UnitOfWork> uowFactory,
            IChatSession chatSession)
        {
            _serviceScopeFactory = serviceScopeFactory;
            _client      = client;
            _uowFactory  = uowFactory;
            _chatSession = chatSession;

            _client.OnUpdate += async(s, e) => await ClientOnOnUpdate(s, e);
        }
예제 #17
0
 public AutoBotEngine(ILogger logger, IChatSession session)
 {
     this.Logger = logger;
     this.Session = session;
     this.Thread = new Thread(delegate()
                              {
                                  while (this.IsRunning)
                                  {
                                      //TODO Add background task implementation here
                                      Thread.Sleep(1000);
                                  }
                                  Thread.CurrentThread.Abort();
                              }
                         );
 }
예제 #18
0
        public ChatBuddies(IChatSession session, BuddyResolver buddyResolver, IEnumerable <IBuddy> buddies)
        {
            this.buddies       = new Dictionary <string, IBuddy>();
            this.buddyResolver = buddyResolver;
            this.session       = session;

            foreach (Buddy buddy in buddies)
            {
                AddBuddy(buddy);
            }

            this.session.GroupChatStarted += session_GroupChatStarted;
            this.session.UserJoined       += session_UserJoined;
            this.session.UserLeft         += session_UserLeft;
        }
예제 #19
0
파일: Chat.cs 프로젝트: dugu01/squiggle
        public Chat(IChatSession session, IBuddy self, IEnumerable <IBuddy> buddies, BuddyResolver buddyResolver, HistoryManager history)
        {
            this.self = self;

            this.buddies                   = new ChatBuddies(session, buddyResolver, buddies);
            this.buddies.BuddyJoined      += buddies_BuddyJoined;
            this.buddies.BuddyLeft        += buddies_BuddyLeft;
            this.buddies.GroupChatStarted += buddies_GroupChatStarted;

            this.session                    = session;
            session.MessageReceived        += session_MessageReceived;
            session.MessageUpdated         += session_MessageUpdated;
            session.UserTyping             += session_UserTyping;
            session.BuzzReceived           += session_BuzzReceived;
            session.ActivityInviteReceived += session_ActivityInviteReceived;

            this.history = history;
        }
예제 #20
0
        public override async Task SendMessageAsync(IChatSession sess, IOutgoingMessage msg)
        {
            var ss = new ChatSession(sess);

            switch (msg.Type)
            {
            case MsgOutType.Text:
            {
                await _cli.SendTextMessageAsync(ss.GetChatId(), msg.Text);
            }
            break;

            case MsgOutType.Keyboard:
            {
                await _cli.SendTextMessageAsync
                (
                    ss.GetChatId(),
                    msg.Text,
                    ParseMode.Markdown,
                    false, false, 0,
                    GetTelegramKeyboard(msg.Keyboard)
                );
            }
            break;

            case MsgOutType.Photo:
                break;

            case MsgOutType.Wait:
            {
                await _cli.SendTextMessageAsync(ss.GetChatId(), $"Нагадування встановлено");

                break;
            }

            default:
                break;
            }
        }
 public Task SendMessageAsync(IChatSession sess, IOutgoingMessage msg)
 {
     return(Task.CompletedTask);
 }
예제 #22
0
        protected async Task ProcessIncomingMessageAsync(IMessageProcessor proc, IIncomingMessage msg, IChatSession sess)
        {
            var ms = proc.ProcessIncomingMessage(sess, msg);

            foreach (var m in ms)
            {
                await SendMessageAsync(sess, m);
            }
        }
예제 #23
0
 protected abstract bool ParseIncomingMessage(String json, out IChatSession sess, out IIncomingMessage msg);
예제 #24
0
        public ChatSession(IChatSession source)
        {
            var ba = source.ChatId.ToByteArray();

            InternalChatId = Convert.ToBase64String(ba);
        }
예제 #25
0
 public abstract Task SendMessageAsync(IChatSession sess, IOutgoingMessage msg);