Skip to content

Commit c080836

Browse files
authored
Merge pull request #5 from textbee/feat/sim-slot-selection
Add sim_slot_index to send_sms
2 parents 0353a78 + 479be9a commit c080836

4 files changed

Lines changed: 59 additions & 9 deletions

File tree

‎README.md‎

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ textbee.send_sms(
4545
| Option | Type | What it does |
4646
| --- | --- | --- |
4747
| `device_id` | `str` | Which phone sends the message. Omit it and textbee uses your default device, or the enabled device with the most recent heartbeat. |
48-
| `sim_subscription_id` | `int` | Which SIM sends the message on a multi-SIM phone. Omit it and the phone uses its configured preferred SIM, or the system default. |
48+
| `sim_slot_index` | `int` | Which SIM sends the message, by its slot in the phone. `0` is SIM 1, `1` is SIM 2. Omit it and the phone uses its default SIM. |
49+
| `sim_subscription_id` | `int` | Deprecated. Use `sim_slot_index`. See [Choosing a SIM](#choosing-a-sim). |
4950
| `scheduled_at` | `str \| datetime` | Send later instead of now. An ISO 8601 string or a timezone-aware `datetime`, in the future. |
5051

5152
```python
@@ -55,7 +56,7 @@ textbee.send_sms(
5556
recipients=["+12025550123"],
5657
message="Your appointment is tomorrow at 9am",
5758
device_id="65f0000000000000000000aa",
58-
sim_subscription_id=2,
59+
sim_slot_index=1,
5960
scheduled_at=datetime.now(timezone.utc) + timedelta(hours=1),
6061
)
6162
```
@@ -74,11 +75,15 @@ if "warning" in result:
7475
print(result["warning"]["message"])
7576
```
7677

77-
### Finding your sim_subscription_id
78+
### Choosing a SIM
7879

79-
Open the textbee Android app, go to **Dashboard**, and find the **SIM Cards** section. Each SIM shows its subscription id with a copy button.
80+
Use `sim_slot_index=0` for SIM 1 and `sim_slot_index=1` for SIM 2. The slot is the place of the SIM in the phone. It stays the same when you take the SIM out and put it back.
8081

81-
Be aware that this value is not validated. If the id does not match a SIM currently in the phone it is ignored, and the message goes out from the preferred or default SIM instead. Nothing errors, so confirm which SIM was used by checking the number the message arrived from.
82+
The slot is the place in the phone, not a specific card. A different card in that slot sends from the new card.
83+
84+
It works with or without `device_id`. If the slot has no active SIM, the message fails with the error code `SIM_SLOT_EMPTY`. textbee never sends it from another SIM. With textbee app versions before slot support, the server refuses a slot it cannot map from the phone's last SIM report with HTTP 400, so the call raises an error.
85+
86+
`sim_subscription_id` still works, but it is deprecated. It is an Android id that changes when a SIM is reinserted. A stale id sends from the default SIM and nothing errors. If you pass both options, textbee uses `sim_slot_index` and ignores the id.
8287

8388
## Devices
8489

‎src/textbee/_client.py‎

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ def send_sms(
8484
message: str,
8585
recipients: Sequence[str],
8686
device_id: str | None = None,
87+
sim_slot_index: int | None = None,
8788
sim_subscription_id: int | None = None,
8889
scheduled_at: DateInput | None = None,
8990
) -> SendSmsResponse:
@@ -101,10 +102,19 @@ def send_sms(
101102
id fails with 400 rather than falling back, so a typo cannot send from
102103
the wrong phone.
103104

104-
``sim_subscription_id`` picks the SIM on a multi-SIM phone. It is not
105-
validated: an id that does not match a SIM in the phone is ignored and
106-
the message goes out from the SIM configured in the app, or the system
107-
default. Find the id in the Android app under Dashboard, "SIM Cards".
105+
``sim_slot_index`` picks the SIM by its slot in the phone: ``0`` is
106+
SIM 1 and ``1`` is SIM 2. It works with or without ``device_id``. If the
107+
slot has no active SIM, the message fails with the error code
108+
``SIM_SLOT_EMPTY`` and is never sent from another SIM. The slot is the
109+
place in the phone, not a specific card: a different card in that slot
110+
sends from the new card. With textbee app versions before slot support,
111+
the server refuses a slot it cannot map from the phone's last SIM report
112+
with HTTP 400, so the call raises.
113+
114+
``sim_subscription_id`` is deprecated: use ``sim_slot_index``. It is an
115+
Android id that changes when a SIM is reinserted, and a stale id sends
116+
from the default SIM with no error. It still works. If you pass both,
117+
textbee uses ``sim_slot_index`` and ignores the id.
108118

109119
``scheduled_at`` sends later instead of now, as an ISO 8601 string or a
110120
timezone-aware ``datetime`` in the future.
@@ -118,6 +128,8 @@ def send_sms(
118128
}
119129
if device_id is not None:
120130
body["deviceId"] = device_id
131+
if sim_slot_index is not None:
132+
body["simSlotIndex"] = sim_slot_index
121133
if sim_subscription_id is not None:
122134
body["simSubscriptionId"] = sim_subscription_id
123135
if scheduled_at is not None:

‎src/textbee/_types.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@ class _MessageFields(_MessageRequired, total=False):
190190
Oldest first. At most 15: the first 5 and the newest 10.
191191
"""
192192

193+
simSlotIndex: int
193194
simSubscriptionId: int
194195
smsBatch: str
195196
createdAt: str

‎tests/test_client.py‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,38 @@ def test_passes_optional_fields_through_and_omits_them_entirely_when_unset(
160160

161161
assert set(http.last.body) == {"message", "recipients"}
162162

163+
def test_sim_slot_index_is_sent_including_zero(self, http: FakeUrlopen) -> None:
164+
http.respond(200, {"data": {"successCount": 1}})
165+
166+
client().send_sms(message="a", recipients=["+15555550123"], sim_slot_index=0)
167+
168+
assert http.last.body == {
169+
"message": "a",
170+
"recipients": ["+15555550123"],
171+
"simSlotIndex": 0,
172+
}
173+
174+
def test_both_sim_options_are_sent(self, http: FakeUrlopen) -> None:
175+
http.respond(200, {"data": {"successCount": 1}})
176+
177+
client().send_sms(
178+
message="both",
179+
recipients=["+15555550123"],
180+
sim_slot_index=1,
181+
sim_subscription_id=2,
182+
)
183+
184+
assert http.last.body["simSlotIndex"] == 1
185+
assert http.last.body["simSubscriptionId"] == 2
186+
187+
def test_no_sim_option_sends_no_sim_keys(self, http: FakeUrlopen) -> None:
188+
http.respond(200, {"data": {"successCount": 1}})
189+
190+
client().send_sms(message="none", recipients=["+15555550123"])
191+
192+
assert "simSlotIndex" not in http.last.body
193+
assert "simSubscriptionId" not in http.last.body
194+
163195
def test_accepts_any_sequence_of_recipients(self, http: FakeUrlopen) -> None:
164196
http.respond(200, {"data": {"successCount": 2}})
165197

0 commit comments

Comments
 (0)