aiogram icon indicating copy to clipboard operation
aiogram copied to clipboard

Different from_users come in payment system messages

Open nleononline opened this issue 1 year ago • 1 comments

Checklist

  • [ ] I am sure the error is coming from aiogram code
  • [ ] I have searched in the issue tracker for similar bug reports, including closed ones

Operating system

macos

Python version

3.13.3

aiogram version

3.20.0.post0

Expected behavior

obviously from_user should behave the same regardless of the message

Current behavior

I got different from_user for messages with successful_payment and refunded_payment.

After a test payment to Stars, I received a message with successful_payment where from_user was my personal account.

After a test payment refund, I received a message with refunded_payment, but from_user no longer pointed to my personal account, but to a bot...

Steps to reproduce

  1. Make a test payment of 1 Stars
  2. Return this payment via refund

message.from_user will be different, which is very strange

Code example

from aiogram import Bot, Dispatcher, F, Router
from aiogram.exceptions import TelegramBadRequest
from aiogram.filters import Command, CommandObject
from aiogram.types import Message, LabeledPrice, PreCheckoutQuery


pay_router = Router()


@pay_router.pre_checkout_query()
async def pre_checkout(query: PreCheckoutQuery):
    await query.answer(ok=True)


@pay_router.message(F.successful_payment)
async def on_successful_payment(message: Message):
    print(message.from_user)
    # >> from_user - your account


@pay_router.message(F.refunded_payment)
async def on_refunded_payment(message: Message):
    print(message.from_user)
    # >> from_user - bot WTF?


@pay_router.message(Command("pay"))
async def command_pay(message: Message):
    await message.answer_invoice(
        title="Test pay",
        description="Test pay description",
        payload="test",
        prices=[LabeledPrice(label="XTR", amount=1)],
        currency="XTR",
    )


@pay_router.message(Command("refund"))
async def command_refund(message: Message, command: CommandObject) -> None:
    if not command.args:
        await message.answer(
            "⚠️ <b>Specify payment ID!</b>\n"
            "\n"
            "Example:\n"
            "/refund 'pay_id'"
        )
        return
    try:
        await message.bot.refund_star_payment(
            user_id=message.from_user.id,
            telegram_payment_charge_id=command.args
        )
    except TelegramBadRequest:
        pass


async def main() -> None:
    bot = Bot(token=...)
    dp = Dispatcher()

    dp.include_router(pay_router)
    await dp.start_polling(bot)


if __name__ == "__main__":
    import asyncio

    asyncio.run(main())

Logs


Additional information

No response

nleononline avatar May 13 '25 19:05 nleononline

@n-leonovets, thanks for reporting this. It doesn't appear to be an issue with Aiogram itself, but rather expected Telegram behavior. When you initiate a purchase in a bot, the from_user field reflects you as the user taking the action. However, when the bot initiates a refund, the from_user field reflects the bot, as it's the bot performing the action. This is logical, as the initiator of the action determines the from_user. I hope this explanation clarifies the behavior you're seeing.

VAIPROG avatar May 13 '25 20:05 VAIPROG