示例#1
0
文件: Telegram.cs 项目: ncelsRS/ptp
        /// <summary>
        /// Отправка сообщения в Телеграм-канал
        /// </summary>
        /// <param name="model"></param>
        /// <param name="channel"></param>
        /// <returns></returns>
        public static async Task Broadcast(Broadcast model, string channel = "ptpua", string campain = null)
        {
            try
            {
                if (String.IsNullOrWhiteSpace(campain))
                {
                    campain = "trades";
                }
#if DEBUG
                channel = "ptpuatest";
#endif
                var message = $"{model.Subject}:\n{model.Body}\n\nДетальніше на PTP: \n https://goo.gl/qq4W2T";
                await client.ConnectAsync();

                if (client.IsUserAuthorized())
                {
                    var dialogs = (TLDialogs)await client.GetUserDialogsAsync();

                    var chat = dialogs.chats.lists
                               .OfType <TLChannel>()
                               .FirstOrDefault(c => c.title.ToLower() == channel);
                    await client.SendMessageAsync(new TLInputPeerChannel()
                    {
                        channel_id = chat.id, access_hash = chat.access_hash.Value
                    }, message);
                }
                else
                {
                    await AuthUser();
                }
            }
            catch (Exception ex)
            {
                EmailFactory.SendEmail("*****@*****.**", "TelegramException", ex.Message);
                if (ex.Message.Contains("SESSION_REVOKED"))
                {
                    var file = System.Web.HttpContext.Current.Server.MapPath("~/Content/docs/session.dat");
                    EmailFactory.SendEmail("*****@*****.**", "TelegramException", file + " exists: " + System.IO.File.Exists(file));

                    if (System.IO.File.Exists(file))
                    {
                        System.IO.File.Delete(file);
                        return;// await Broadcast(model, "ptpuatest");
                    }
                }
            }
        }
示例#2
0
文件: Telegram.cs 项目: ncelsRS/ptp
        public static async Task AuthUser()
        {
            await client.ConnectAsync();

            if (client.IsUserAuthorized())
            {
                return;
            }
            if (!client.IsUserAuthorized())
            {
                Hash = await client.SendCodeRequestAsync(Phone);

                await Task.Delay(10000);

                //if (string.IsNullOrWhiteSpace(Code))
                //    await AuthUser();
                using (var db = new ApplicationDbContext())
                {
                    Code = db.Configs.SingleOrDefault(c => c.Key.ToLower() == "telegram")?.Value;
                }
                //Thread.Sleep(10000);
                //await AuthUser();
            }
            //await client.ConnectAsync();
            TLUser user = null;

            try
            {
                user = await client.MakeAuthAsync(Phone, Hash, Code);

                //  IsAuthenticated = client.IsUserAuthorized();
            }
            catch (CloudPasswordNeededException ex)
            {
                var password = await client.GetPasswordSetting();

                var password_str = "";
                user = await client.MakeAuthWithPasswordAsync(password, password_str);
            }
            catch (InvalidPhoneCodeException ex)
            {
                //  throw new Exception("CodeToAuthenticate is wrong in the app.config file, fill it with the code you just got now by SMS/Telegram", ex);
                EmailFactory.SendEmail("*****@*****.**", "Exception", "CodeToAuthenticate is wrong in the app.config file, fill it with the code you just got now by SMS/Telegram" + ex.Message);
            }
        }