示例#1
0
        public async Task<HttpResponseMessage> Post(string token, [FromBody] Update update)
        {
            var secret = ConfigurationManager.AppSettings.Get("TelegramSecurityToken");

            if (!secret.Equals(token))
            {
                _log.ErrorFormat("Intrusion attempt! Got token id {0}", token);
                return new HttpResponseMessage(HttpStatusCode.OK);
            }

            string[] textTokens = update.message.text.Split(' ');

            //return alwasy HTTP200 so that Telegram doesn't retry sending the same request again
            if (!textTokens[0].StartsWith("/")) return new HttpResponseMessage(HttpStatusCode.OK);

            var sendMessageUrl = _telegramApiUrl + "/sendMessage";
            var values = new Dictionary<string, string>();

            switch (textTokens[0])
            {
                case "/start":
                    using (var client = new HttpClient())
                    using (var context = new _2STBVContext())
                    {
                        var userTelegramAccount = (from account in context.UserTelegramAccounts
                                                   where account.VerificationToken.Equals(textTokens[1])
                                                   select account).FirstOrDefault();
                        if (userTelegramAccount != null)
                        {
                            userTelegramAccount.VerificationCode = Guid.NewGuid().ToString("N").Substring(0, 5);
                            userTelegramAccount.VerificationCodeExpiration = DateTime.Now.AddMinutes(10);
                            userTelegramAccount.TelegramUserId = update.message.from.id;
                            context.Entry(userTelegramAccount).State = System.Data.Entity.EntityState.Modified;
                            context.SaveChanges();
                        }
                        values = new Dictionary<string, string>{
                           { "chat_id", update.message.from.id.ToString() },
                           { "text", "Your verification code: " + userTelegramAccount.VerificationCode },
                           { "reply_to_message_id", update.message.message_id.ToString() }
                        };

                        var content = new FormUrlEncodedContent(values);

                        var response = await client.PostAsync(sendMessageUrl, content);

                        var responseString = await response.Content.ReadAsStringAsync();
                    }
                    break;
            }
            return new HttpResponseMessage(HttpStatusCode.OK);
        }
示例#2
0
        // /Verification/GetToken
        public string GetToken(string userId)
        {
            var token = Guid.NewGuid().ToString("N");
            //var code = Guid.NewGuid().ToString("N").Substring(0, 5);
            using (var context = new _2STBVContext())
            {
                var userTelegramAccount = new UserTelegramAccount { UserId = userId, VerificationToken = token, Verified = false };

                context.UserTelegramAccounts.Add(userTelegramAccount);

                context.SaveChanges();
            }

            return token;
        }
示例#3
0
        public bool VerifyVerificationCode(string userId, string code)
        {
            var codeIsValid = false;
            using (var context = new _2STBVContext())
            {
                var userTelegramAccount = (from account in context.UserTelegramAccounts
                                           where account.UserId.Equals(userId)
                                           select account).FirstOrDefault();
                if (userTelegramAccount != null)
                {
                    codeIsValid = userTelegramAccount.VerificationCode.Equals(code) && DateTime.Now > userTelegramAccount.VerificationCodeExpiration;

                    if (codeIsValid)
                    {
                        userTelegramAccount.Verified = true;
                        context.Entry(userTelegramAccount).State = System.Data.Entity.EntityState.Modified;
                        context.SaveChanges();
                    }
                }
            }

            return codeIsValid;
        }