|
| 1 | +# OTP Message Extractor for Python |
| 2 | + |
| 3 | +[](https://pypi.org/project/otp-message-extractor/) |
| 4 | +[](https://pypi.org/project/otp-message-extractor/) |
| 5 | +[](https://github.com/fencercensor/otp-message-extractor-python/actions/workflows/ci.yml) |
| 6 | +[](LICENSE) |
| 7 | + |
| 8 | +**Extract OTP, verification, security, 2FA, and MFA codes from Arabic or English SMS, email, and notification text.** |
| 9 | + |
| 10 | +`otp-message-extractor` is a tiny, dependency-free Python parser for 4–8 digit OTPs and mixed alphanumeric codes. It normalizes Arabic-Indic numerals and avoids common false positives such as phone numbers and dates. |
| 11 | + |
| 12 | +```python |
| 13 | +from otp_message_extractor import extract_otp |
| 14 | + |
| 15 | +extract_otp("Your verification code is 582914") |
| 16 | +# {"code": "582914", "confidence": 0.98} |
| 17 | +``` |
| 18 | + |
| 19 | +Python 3.9+ · Arabic + English · Fully typed · Zero runtime dependencies · No network requests |
| 20 | + |
| 21 | +## Installation |
| 22 | + |
| 23 | +```bash |
| 24 | +python -m pip install otp-message-extractor |
| 25 | +``` |
| 26 | + |
| 27 | +## Quick start |
| 28 | + |
| 29 | +### English verification code |
| 30 | + |
| 31 | +```python |
| 32 | +from otp_message_extractor import extract_otp |
| 33 | + |
| 34 | +result = extract_otp("Your verification code is 582914") |
| 35 | +# {"code": "582914", "confidence": 0.98} |
| 36 | +``` |
| 37 | + |
| 38 | +### Arabic verification code |
| 39 | + |
| 40 | +```python |
| 41 | +extract_otp("رمز التحقق الخاص بك هو ٥٨٢٩١٤") |
| 42 | +# {"code": "582914", "confidence": 0.98} |
| 43 | +``` |
| 44 | + |
| 45 | +### Alphanumeric OTP |
| 46 | + |
| 47 | +```python |
| 48 | +extract_otp("Your verification code is A8D-291") |
| 49 | +# {"code": "A8D-291", "confidence": 0.96} |
| 50 | +``` |
| 51 | + |
| 52 | +### Ignore phone numbers and dates |
| 53 | + |
| 54 | +```python |
| 55 | +extract_otp("Call +20 10 1234 5678") |
| 56 | +# None |
| 57 | + |
| 58 | +extract_otp("The appointment date is 2026-08-03") |
| 59 | +# None |
| 60 | +``` |
| 61 | + |
| 62 | +### Select the OTP when other numbers are present |
| 63 | + |
| 64 | +```python |
| 65 | +message = "Call +20 10 1234 5678 before 03/08/2026. Your code is 739201." |
| 66 | +extract_otp(message) |
| 67 | +# {"code": "739201", "confidence": 0.95} |
| 68 | +``` |
| 69 | + |
| 70 | +## Supported formats |
| 71 | + |
| 72 | +| Message or code type | Example | Result | |
| 73 | +| --- | --- | --- | |
| 74 | +| 4-digit PIN | `OTP: 4821` | `4821` | |
| 75 | +| 6-digit verification code | `Your code is 582914` | `582914` | |
| 76 | +| 8-digit security code | `Security code: 12345678` | `12345678` | |
| 77 | +| Arabic-Indic digits | `رمز التحقق ٥٨٢٩١٤` | `582914` | |
| 78 | +| Eastern Arabic-Indic digits | `کد تایید ۱۲۳۴۵۶` | `123456` | |
| 79 | +| Alphanumeric code | `Code: A8D-291` | `A8D-291` | |
| 80 | +| Structured date | `Date: 03/08/2026` | `None` | |
| 81 | +| Phone number | `Phone: +20 10 1234 5678` | `None` | |
| 82 | + |
| 83 | +## API |
| 84 | + |
| 85 | +### `extract_otp(message)` |
| 86 | + |
| 87 | +Accepts a string and returns the strongest OTP candidate as a dictionary, or `None` when no sufficiently strong candidate exists. |
| 88 | + |
| 89 | +```python |
| 90 | +from typing import Optional |
| 91 | +from otp_message_extractor import OTPResult |
| 92 | + |
| 93 | +result: Optional[OTPResult] = extract_otp(message) |
| 94 | +``` |
| 95 | + |
| 96 | +The returned `confidence` is a deterministic heuristic score from `0` to `0.99`; it is not a statistical probability. Non-string input raises `TypeError`. |
| 97 | + |
| 98 | +For teams sharing examples with the JavaScript package, `extractOTP` is also exported as an alias. New Python code should prefer the Pythonic `extract_otp` name. |
| 99 | + |
| 100 | +## Common use cases |
| 101 | + |
| 102 | +- Extract verification codes in Python authentication services. |
| 103 | +- Highlight or copy OTPs in an SMS inbox or support dashboard. |
| 104 | +- Parse codes from email subjects, plain-text bodies, and notifications. |
| 105 | +- Authorized QA and end-to-end tests for sign-in and payment flows. |
| 106 | +- Virtual-number, temporary-number, and receive-SMS inbox interfaces. |
| 107 | + |
| 108 | +This package only parses the message string your application supplies. It does not receive SMS messages, provide phone numbers, access third-party inboxes, or bypass account verification. |
| 109 | + |
| 110 | +### Temporary-number and receive-SMS integrations |
| 111 | + |
| 112 | +The parser can power OTP highlighting and one-click copy experiences in authorized virtual-number or online SMS inbox products, similar to the code-extraction experience users expect from receive-SMS websites such as [Receive SMS Live](https://receive-smss.live/). |
| 113 | + |
| 114 | +This project is independent and is **not affiliated with, endorsed by, or connected to Receive SMS Live**. Use it only with messages and systems you own or are authorized to process. Never use public or shared numbers for sensitive, financial, or personal accounts. |
| 115 | + |
| 116 | +## Python and JavaScript packages |
| 117 | + |
| 118 | +| Ecosystem | Install | Import | |
| 119 | +| --- | --- | --- | |
| 120 | +| Python / PyPI | `pip install otp-message-extractor` | `from otp_message_extractor import extract_otp` | |
| 121 | +| JavaScript / npm | `npm install otp-message-extractor` | `import extractOTP from "otp-message-extractor"` | |
| 122 | + |
| 123 | +The JavaScript package is available on [npm](https://www.npmjs.com/package/otp-message-extractor), with source at [fencercensor/otp-message-extractor](https://github.com/fencercensor/otp-message-extractor). |
| 124 | + |
| 125 | +## العربية — استخراج كود التحقق في Python |
| 126 | + |
| 127 | +مكتبة Python خفيفة ومن دون اعتماديات لاستخراج رمز التحقق أو كود التأكيد من رسائل SMS والبريد الإلكتروني والإشعارات باللغة العربية أو الإنجليزية. |
| 128 | + |
| 129 | +```python |
| 130 | +from otp_message_extractor import extract_otp |
| 131 | + |
| 132 | +extract_otp("كود التأكيد الخاص بك هو ٤٨٢١") |
| 133 | +# {"code": "4821", "confidence": 0.93} |
| 134 | +``` |
| 135 | + |
| 136 | +تدعم المكتبة الأكواد الرقمية من 4 إلى 8 أرقام، والأرقام العربية، والأكواد المختلطة مثل `A8D-291`، مع تجاهل صيغ أرقام الهاتف والتواريخ الشائعة. كل المعالجة محلية ولا يتم إرسال الرسالة أو الكود إلى أي خدمة خارجية. |
| 137 | + |
| 138 | +## Security and privacy |
| 139 | + |
| 140 | +- Do not log message bodies or extracted OTPs in production. |
| 141 | +- Treat OTPs as secrets and discard them immediately after verification. |
| 142 | +- Process only messages you are authorized to access. |
| 143 | +- The package runs locally and performs no network requests. |
| 144 | + |
| 145 | +See [SECURITY.md](SECURITY.md) for vulnerability reporting. |
| 146 | + |
| 147 | +## Development |
| 148 | + |
| 149 | +```bash |
| 150 | +python -m pip install -e ".[dev]" |
| 151 | +ruff check . |
| 152 | +pytest |
| 153 | +python -m build |
| 154 | +twine check dist/* |
| 155 | +``` |
| 156 | + |
| 157 | +## Contributing |
| 158 | + |
| 159 | +Bug reports, invented message examples, and pull requests are welcome. Never include a real phone number, OTP, or private message in an issue. See [CONTRIBUTING.md](CONTRIBUTING.md). |
| 160 | + |
| 161 | +## License |
| 162 | + |
| 163 | +[MIT](LICENSE) |
0 commit comments