Exemplo n.º 1
0
        public static async Task <List <Result> > getUpdates(TelegramService service, int updateId)
        {
            var result = await service.PostRequestAsync <UpdateResult>("getUpdates", new GetUpdates { offset = updateId });

            if (result == null || result.result == null)
            {
                throw new System.Exception("GetUpdates returned null");
            }
            return(result.result);
        }
Exemplo n.º 2
0
        public static async Task sendMessageWithReplyOptions(TelegramService service, string message, long chatId, List <string> replyOptions)
        {
            var keyboardReply = generateKeyboard(replyOptions);

            if (keyboardReply != null)
            {
                await service.PostRequestAsync <object>("sendMessage", new MessageSendWithKeyboard { chat_id = chatId, text = message, reply_markup = keyboardReply });
            }
            else
            {
                await service.PostRequestAsync <object>("sendMessage", new MessageSend { chat_id = chatId, text = message });
            }
        }
        public async void RecieveNewMessage(Message message, TelegramService service)
        {
            if (message == null || message.text == null)
            {
                return;
            }
            Log.LogMessage("Recivied new message: " + message.text, LogType.General);

            string chatName = message.chat.username;

            if (string.IsNullOrWhiteSpace(chatName))
            {
                chatName = message.chat.first_name;
            }

            var playerState = _playerDatabaseController.GetPlayerByTelegramChannel(message.chat.id);

            if (playerState == null)
            {
                Log.LogMessage("Instance was not found for id: " + message.chat.id + " Creating new game instance", LogType.General);
                _reporter.ReportMessage("The bot was started in a new chat, with username: "******"Existing instance found for chat: " + message.chat.id + " Sending message", LogType.General);
            }
            var executionResult = _gameExecutor.ProcessNewMessage(message.text, new PlayerState {
                player = playerState
            });

            if (executionResult != null)
            {
                foreach (var a in executionResult.MessagesToShow)
                {
                    if (!string.IsNullOrWhiteSpace(a.ImageUrl))
                    {
                        await Methods.sendImageWithReplyOptions(service, a.ImageUrl, message.chat.id, executionResult.OptionsToShow);
                    }
                    else if (!string.IsNullOrWhiteSpace(a.Message))
                    {
                        await Methods.sendMessageWithReplyOptions(service, a.Message, message.chat.id, executionResult.OptionsToShow);
                    }
                }
                ;
                if (executionResult.IsInvalidInput)
                {
                    await Methods.sendMessage(service, "That's not a valid input!", message.chat.id);
                }
            }
        }
Exemplo n.º 4
0
 public static async Task <User> getMe(TelegramService service)
 {
     return(await service.PostRequestAsync <User>("getMe", null));
 }
Exemplo n.º 5
0
 public static async Task sendImageWithReplyOptions(TelegramService service, string imageUrl, long chatId, List <string> replyOptions)
 {
     var keyboardReply = generateKeyboard(replyOptions);
     await service.PostRequestAsync <object>("sendPhoto", new PhotoSendWithKeyboard { chat_id = chatId, photo = imageUrl, reply_markup = keyboardReply });
 }
Exemplo n.º 6
0
 public static async Task sendMessage(TelegramService service, string message, long chatId)
 {
     await service.PostRequestAsync <object>("sendMessage", new MessageSend { chat_id = chatId, text = message });
 }
Exemplo n.º 7
0
 public UpdateListener(TelegramService service)
 {
     _service = service;
 }
Exemplo n.º 8
0
 public TelegramCommunicator(IConfigurationService configService, IPlayerDatabaseController playerDatabaseController,
                             IReporter reporter, IAccountController accountController, IGoogleDriveService gdriveService, IGameExecutor gameExecutor)
 {
     _service  = new TelegramService(configService.GetConfig("TelegramUrl"), configService.GetConfig("TelegramApiKey", true));
     _reporter = reporter;
 }