Customers paste card numbers into the chat. If your program sends a message on as it is, the number travels to the model's provider, into your logs and even into the answers. The simplest rule to keep is that a card number never leaves your server: every message goes through a filter before the model call, and the filter puts [card] in the number's place.
Finding what could be a card: a regular expression describes a shape, and re.findall returns every part of the text that fits it, as a list. Here the shape is three digits in a row:
re.findall(r"\d{3}", "Room 204, desk 117") # ['204', '117']
The Luhn check tells a card number from any other long number. Walk the digits from the last one to the first. Double every second digit, and if that gives more than 9, take 9 away. Add everything up: the number passes when the total ends in 0. For 7526: 6, then 2 doubled is 4, then 5, then 7 doubled is 14, which counts as 5. The total is 6 + 4 + 5 + 5 = 20, so 7526 passes.
When your filter works, the program prints this. The order number 1042 is too short to be a card, and the tracking number fails the Luhn check, so both stay:
Sent: My payment for order 1042 failed. The card is [card], can you try again? Shop: I can't take payments in this chat. Please try again on the checkout page for order 1042. Sent: Then use my other card: [card]. Shop: Please enter your other card on the checkout page, never in this chat. Sent: I paid with [card]. The tracking number is 1234567890123456, where is my parcel? Shop: Parcel 1234567890123456 left our warehouse on Monday and should arrive on Friday. Model calls: 3 Card numbers the model saw: 0
Your task: the client, the inbox and the pattern CARD are already there. Pass every message through remove_cards before the model call. In remove_cards, find every part that could be a card and replace the ones that have 13 to 19 digits and pass the check with [card]. Then finish passes_luhn, one rule at a time.
Assignment
Follow the Coding Tutorial to build the card filter and the Luhn check. Run the program after each step and watch what the model gets.
Hintposition is 0 at the last digit, so the digits to double are at the odd positions. Only a doubled digit can be above 9, so that test goes inside the doubling if.
Free plan: 30 tutor messages. PRO is unlimited.
