コード例 #1
0
ファイル: TelegramExt.cs プロジェクト: solarin/Utilities
        public static async Task <Message?> SendInvoiceAsync(this ITelegramBotClient bot, TelegramChatStatusDesc c, string?title, string?description, string?payload, string?providertoken
                                                             , string?startParameter
                                                             , string?currency
                                                             , IEnumerable <LabeledPrice> prices
                                                             , string?providerData = null
                                                             , InlineKeyboardMarkup replyMarkup = null
                                                             , bool needPhone           = false
                                                             , bool needEmail           = false
                                                             , bool needShippingAddress = false
                                                             , bool needName            = false
                                                             , string?photoUrl          = null
                                                             , CancellationToken ct     = default(CancellationToken))
        {
            if (bot == null)
            {
                return(null);
            }
            if (c == null)
            {
                return(null);
            }
            int editMessageId = c.LastMessageId;


            if (c.DeleteMessage)
            {
                if (await bot.DeleteMessageIdAsync(c, editMessageId))
                {
                    c.AnswerToMessageId = 0;
                }
                //try
                //{
                //	await bot.DeleteMessageAsync(c.ChatId, editMessageId, ct);
                //	c.AnswerToMessageId = 0;
                //}
                //catch (Exception exx)
                //{
                //}
            }

            if (c.ChatId?.Identifier == null)
            {
                log.Warn($"{c},{c.ChatId} has null identifier");
            }

            try
            {
                var invoice = await bot.SendInvoiceAsync(c.ChatId.Identifier ?? 0
                                                         , title : title
                                                         , description : description
                                                         , payload : payload
                                                         , providerToken : providertoken
                                                         , startParameter : startParameter
                                                         , currency : currency
                                                         , prices : prices
                                                         , providerData : providerData
                                                         , replyMarkup : replyMarkup
                                                         , needPhoneNumber : needPhone
                                                         , needEmail : needEmail
                                                         , needShippingAddress : needShippingAddress
                                                         , needName : needName
                                                         , photoUrl : photoUrl);

                if (invoice == null)
                {
                    return(null);
                }

                c.LastMessageId = invoice.MessageId;
                c.DeleteMessage = true; //next time this message must be deleted, because we cant edit an invoice message
                return(invoice);
            }
            catch (Exception e)
            {
                c.DeleteMessage = false;
                log.Debug($"Bot: {bot.BotId}, to: {c.ChatId} - Error SendInvoiceAsync", e);
                return(null);
            }
        }
コード例 #2
0
ファイル: TelegramExt.cs プロジェクト: solarin/Utilities
        public static async Task <Message?> SendOrUpdateMessage(this ITelegramBotClient bot, TelegramChatStatusDesc desc, string?text, IReplyMarkup replyMarkup = null, bool onlysend = false, CancellationToken ct = default(CancellationToken))
        {
            if (bot == null)
            {
                return(null);
            }
            if (desc == null)
            {
                return(null);
            }
            await bot.DeleteMessageIdAsync(desc, desc.DeleteAlsoMessageId);

            desc.DeleteAlsoMessageId = 0;
            int deleteNext = 0;

            if (onlysend)
            {
                // we save the previous message that will be deleted next time, so the next sent message will be under this one that we are sending now
                deleteNext         = desc.LastMessageId;
                desc.LastMessageId = 0;
            }
            int editMessageId = desc.LastMessageId;

            // try to edit/delete old message
            InlineKeyboardMarkup inreplyMarkup = replyMarkup as InlineKeyboardMarkup;

            if (editMessageId != 0 && (replyMarkup == null || inreplyMarkup != null))
            {
                if (desc.DeleteMessage == false)
                {
                    try
                    {
                        //System.Diagnostics.Debug.WriteLine($"1. msg to: {desc.ChatId.Identifier}, {text}");
                        var msgout = await bot.EditMessageTextAsync(desc.ChatId, editMessageId, text, parseMode : ParseMode.Html, disableWebPagePreview : true, replyMarkup : inreplyMarkup, cancellationToken : ct);

                        //System.Diagnostics.Debug.WriteLine($"2. msg to: {desc.ChatId.Identifier}, {text}");
                        return(msgout);
                    }
                    catch (ApiRequestException ar)
                    {
                        if (ar.Message.Contains("message is not modified"))
                        {
                            return(null);
                        }
                        else
                        {
                            log.Debug($"{desc}", ar);
                        }
                    }
                }
コード例 #3
0
ファイル: TelegramExt.cs プロジェクト: solarin/Utilities
        public static async Task <Message?> SendPhotoAsync(this ITelegramBotClient bot, TelegramChatStatusDesc desc, string?caption, InputOnlineFile file, IReplyMarkup replyMarkup = null, CancellationToken ct = default(CancellationToken))
        {
            if (bot == null)
            {
                return(null);
            }
            if (desc == null)
            {
                return(null);
            }
            int editMessageId = desc.LastMessageId;


            if (desc.DeleteMessage)
            {
                if (await bot.DeleteMessageIdAsync(desc, editMessageId))
                {
                    desc.AnswerToMessageId = 0;
                }
            }
            try
            {
                var photo = await bot.SendPhotoAsync(desc.ChatId, file, caption, ParseMode.Html, replyMarkup : replyMarkup);

                desc.LastMessageId = photo.MessageId;
                desc.DeleteMessage = true; //next time this message must be deleted, because updating the caption of a photo with somme random content doesn't make much sense
                return(photo);
            }
            catch (Exception e)
            {
                desc.DeleteMessage = false;
                log.Debug($"Bot: {bot.BotId}, To: {desc.ChatId} - Error SendPhotoAsync", e);
                return(null);
            }
        }