19 "go.mau.fi/whatsmeow/proto/waE2E"
20 "go.mau.fi/whatsmeow/store"
21 "go.mau.fi/whatsmeow/store/sqlstore"
22 "go.mau.fi/whatsmeow/types"
23 "go.mau.fi/whatsmeow/types/events"
24 waLog "go.mau.fi/whatsmeow/util/log"
26 // The session store of the library opens SQLite with this pure Go
28 _ "modernc.org/sqlite"
32 // whatsappDirName is the directory of the WhatsApp files in the data
34 whatsappDirName = "whatsapp"
36 // whatsappSessionFileName is the session database of the library in
38 whatsappSessionFileName = "session.db"
40 // whatsappCommand starts the input lines of the WhatsApp commands, such
41 // as "/whatsapp status".
42 whatsappCommand = "/whatsapp"
44 // whatsappUsage is the answer to a WhatsApp command that does not
46 whatsappUsage = "Use /whatsapp pair PHONE, /whatsapp status, or /whatsapp unlink. PHONE is the number of your WhatsApp account with its country code, such as +15550001111."
48 // whatsappPairSteps tells the user what to do with the pairing code,
49 // which comes in the next message.
50 whatsappPairSteps = "In WhatsApp on your phone, open Linked devices, tap Link a device, then tap Link with phone number instead, and enter the code of the next message. The code expires in about 2 minutes."
52 // whatsappPairExpired is the answer when the pairing code expires.
53 whatsappPairExpired = "The pairing code expired. Run /whatsapp pair PHONE again for a new code."
55 // whatsappTimeLayout is the layout of the times in the answers.
56 whatsappTimeLayout = "2006-01-02 15:04"
58 // whatsappPairTime is the longest time of a pairing. The library closes
59 // the pairing connection after about 160 seconds.
60 whatsappPairTime = 3 * time.Minute
62 // whatsappLogoutTime is the longest time of the logout call of an
64 whatsappLogoutTime = 30 * time.Second
66 // whatsappNoticeDelay is how long a disconnection lasts before the user
68 whatsappNoticeDelay = 10 * time.Minute
70 // whatsappRequests is the number of pair and unlink commands that can
74 // whatsappLookupTime is the longest time of a read of the names in the
75 // session database or of the groups from WhatsApp.
76 whatsappLookupTime = 10 * time.Second
78 // whatsappNotLinked is the answer of a WhatsApp tool without a link.
79 whatsappNotLinked = "WhatsApp is not linked. Tell the user to run /whatsapp pair PHONE in the terminal or in Telegram."
81 // whatsappSendTime is the longest time of a send.
82 whatsappSendTime = 30 * time.Second
84 // whatsappSendBytes is the most bytes of the text of a send.
85 whatsappSendBytes = 65536
87 // whatsappSendIDBytes is the most bytes of the ID of a send.
88 whatsappSendIDBytes = 100
90 // The states of a send record.
91 whatsappSending = "sending"
93 whatsappFailed = "failed"
96 // WhatsAppClient is the part of the WhatsApp client that the daemon uses. The
97 // real one is a *whatsmeow.Client from whatsappNewClient, and the tests use a
99 type WhatsAppClient interface {
100 ConnectContext(ctx context.Context) error
102 GetQRChannel(ctx context.Context) (<-chan whatsmeow.QRChannelItem, error)
103 PairPhone(ctx context.Context, phone string, showPushNotification bool, clientType whatsmeow.PairClientType, clientDisplayName string) (string, error)
104 Logout(ctx context.Context) error
105 GetJoinedGroups(ctx context.Context) ([]*types.GroupInfo, error)
106 GenerateMessageID() types.MessageID
107 SendMessage(ctx context.Context, to types.JID, message *waE2E.Message, extra ...whatsmeow.SendRequestExtra) (whatsmeow.SendResponse, error)
110 // openWhatsApp opens the WhatsApp session in the data directory dir. Without
111 // the session file, WhatsApp is not linked. The session gives full access to
112 // the account, so a file that another user can read is an error. The clock
113 // gives the times of the status, logger gets the failures, and newClient makes
114 // the client of a device with its event handler.
115 func openWhatsApp(ctx context.Context, dir string, clock func() time.Time, logger *log.Logger, newClient func(device *store.Device, handle func(event any) bool) WhatsAppClient) (*WhatsApp, error) {
117 dir: filepath.Join(dir, whatsappDirName),
120 newClient: newClient,
121 noticeDelay: whatsappNoticeDelay,
122 requests: make(chan WhatsAppRequest, whatsappRequests),
123 logouts: make(chan struct{}, 1),
124 groups: newWhatsAppGroups(logger),
128 w.state, err = openWhatsAppState(filepath.Join(w.dir, whatsappStateFileName), whatsappBatchQuiet, whatsappBatchWindow)
133 w.inbox = newWhatsAppInbox(w.state, clock, w.groups)
135 info, err := os.Stat(w.sessionPath())
136 if errors.Is(err, os.ErrNotExist) {
141 return nil, fmt.Errorf("read WhatsApp session: %w", err)
144 if info.Mode().Perm()&0o077 != 0 {
145 return nil, fmt.Errorf("%s holds the WhatsApp session, so its mode must be 0600", filepath.Join(whatsappDirName, whatsappSessionFileName))
148 err = w.openSession(ctx)
153 if w.device.ID != nil {
155 w.inbox.waitForCatchup()
156 w.own = whatsappOwnIDs(w.device.ID.User, w.device.LID.User)
162 // WhatsApp is the link of the daemon to the WhatsApp account of the user, as a
163 // linked device. The session database of the library in dir holds the keys of
164 // the device. Run keeps a linked device connected, and the library connects
165 // again by itself after a network failure.
167 // The commands pair and unlink go to Run as requests, and Run runs one at a
168 // time. A pairing runs in its own goroutine until its code expires, and a new
169 // pairing ends the one before. Its phone number and code go only to the answer
170 // of the client that asked. A logout from the phone comes as an event of the
171 // library, and Run deletes the session. The mutex protects the session, the
172 // client, and the state of the connection, which the event handler of the
173 // library changes from its own goroutines.
175 // The inbox puts the text messages into batches and logs them, and groups
176 // holds the names of the groups. Both get a WhatsAppLink from link for each
177 // call, so they use the current device and client.
179 // The library logs nothing, because its log lines hold the ID of the own
180 // account, which is the phone number of the user. The own number and the own
181 // LID never go to the log of the agent or to the log of the daemon: redact
182 // removes them from the errors, and a batch shows the messages of the user as
183 // You and a mention of the user as @you. The IDs of the other chats go to the
184 // log, and those of private chats hold phone numbers, because the agent needs
186 type WhatsApp struct {
188 clock func() time.Time
190 newClient func(device *store.Device, handle func(event any) bool) WhatsAppClient
191 noticeDelay time.Duration
192 requests chan WhatsAppRequest
193 logouts chan struct{}
195 groups *WhatsAppGroups
199 container *sqlstore.Container
201 client WhatsAppClient
206 connectedAt time.Time
207 disconnectedAt time.Time
214 // Run connects a linked device, logs the closed batches in l, runs the pair and
215 // unlink commands, and handles a logout from the phone, until ctx ends. Its
216 // notes go to the log of l, and notify gets the notices for all clients. A
217 // failed first connection is logged, and the library tries again when the
218 // failure is one of the network. Run returns the error of a write of the log or
219 // of the state file, which stops the daemon, because then messages would be
221 func (w *WhatsApp) Run(ctx context.Context, l *TurnLoop, notify func(Event)) error {
231 var pairing sync.WaitGroup
240 err := w.inbox.deliver(ctx, l, w.link(), false)
248 case err = <-w.inbox.failures:
251 case <-w.inbox.due():
253 err = w.loggedOut(ctx, l)
257 case request := <-w.requests:
264 err = w.unlink(ctx, l, request.answer)
269 request.answer("WhatsApp is linked already. Run /whatsapp unlink first.")
271 pairCtx, cancelPair := context.WithTimeout(ctx, whatsappPairTime)
276 w.pair(pairCtx, ctx, l, request.phone, request.answer)
283 // Command runs the WhatsApp command args, the text after /whatsapp, and gives
284 // each answer to answer. The answers go only to the client that sent the
286 func (w *WhatsApp) Command(args string, answer func(text string)) {
287 fields := strings.Fields(args)
288 request := WhatsAppRequest{
293 case len(fields) == 1 && fields[0] == "status":
296 case len(fields) == 1 && fields[0] == "unlink":
297 request.unlink = true
298 case len(fields) >= 2 && fields[0] == "pair":
299 phone, valid := whatsappPhone(strings.Join(fields[1:], ""))
301 answer("PHONE must be the number of your WhatsApp account with its country code: 7 to 15 digits, such as +15550001111.")
305 request.phone = phone
307 answer(whatsappUsage)
312 case w.requests <- request:
314 answer("Too many WhatsApp commands wait. Try again in a minute.")
318 // Status returns the state of the link: not linked, linked and connected, or
319 // linked and disconnected, with the times of the connection. It never holds the
321 func (w *WhatsApp) Status() string {
323 defer w.mutex.Unlock()
327 return "WhatsApp is not linked. Run /whatsapp pair PHONE to link it."
329 return "WhatsApp is linked and connected since " + w.connectedAt.Format(whatsappTimeLayout) + "."
330 case w.disconnectedAt.IsZero():
331 return "WhatsApp is linked and not connected yet."
332 case w.connectedAt.IsZero():
333 return "WhatsApp is linked and disconnected since " + w.disconnectedAt.Format(whatsappTimeLayout) + "."
335 return "WhatsApp is linked and disconnected since " + w.disconnectedAt.Format(whatsappTimeLayout) + ". The last connection started at " + w.connectedAt.Format(whatsappTimeLayout) + "."
339 // Close closes the session database.
340 func (w *WhatsApp) Close() error {
342 defer w.mutex.Unlock()
344 if w.container == nil {
348 err := w.container.Close()
354 // Find returns the answer of whatsapp_find for name, as whatsappFind says.
355 func (w *WhatsApp) Find(ctx context.Context, name string) (string, error) {
356 return whatsappFind(ctx, w.link(), w.groups, name)
359 func (w *WhatsApp) sessionPath() string {
360 return filepath.Join(w.dir, whatsappSessionFileName)
363 // openSession opens the session database and its first device. The caller holds
364 // the mutex, or no other goroutine uses w yet.
365 func (w *WhatsApp) openSession(ctx context.Context) error {
366 address, err := whatsappSessionAddress(w.sessionPath())
371 container, err := sqlstore.New(ctx, "sqlite", address, waLog.Noop)
373 return fmt.Errorf("open WhatsApp session: %w", err)
376 device, err := container.GetFirstDevice(ctx)
378 return errors.Join(fmt.Errorf("read WhatsApp device: %w", err), container.Close())
381 w.container = container
387 func (w *WhatsApp) isLinked() bool {
389 defer w.mutex.Unlock()
394 // link returns a copy of the link of the device: whether it is linked, the
395 // device, the client, whether it is connected, and the own IDs.
396 func (w *WhatsApp) link() WhatsAppLink {
398 defer w.mutex.Unlock()
404 connected: w.connected,
405 own: slices.Clone(w.own),
409 // connect makes the client of the linked device and connects it.
410 func (w *WhatsApp) connect(ctx context.Context) {
412 w.client = w.newClient(w.device, w.handle)
416 err := client.ConnectContext(ctx)
418 w.logger.Printf("connect: %s", w.redact(err.Error()))
422 // close ends the connection and the timer of the notice, and stops all notices.
423 func (w *WhatsApp) close() {
441 // loggedOut logs the open batches, deletes the session after a logout from the
442 // phone, notes it in the log of l, and tells all clients one time.
443 func (w *WhatsApp) loggedOut(ctx context.Context, l *TurnLoop) error {
448 err := w.inbox.deliver(ctx, l, w.link(), true)
453 err = w.removeSession()
455 w.logger.Printf("delete the session after the logout: %v", err)
458 err = l.note("WhatsApp was unlinked from the phone.")
460 w.logger.Printf("note the logout: %v", err)
463 w.broadcast("WhatsApp was unlinked from the phone. Run /whatsapp pair PHONE to link it again.")
468 // unlink logs the open batches, logs out the linked device, and deletes the
469 // session. When the logout call fails, as it does while WhatsApp is
470 // disconnected, the session is deleted all the same, and answer tells the user
471 // to remove the device in the phone. Its error is one of the log or the state
473 func (w *WhatsApp) unlink(ctx context.Context, l *TurnLoop, answer func(text string)) error {
480 answer("WhatsApp is not linked.")
484 err := w.inbox.deliver(ctx, l, w.link(), true)
491 ctx, cancel := context.WithTimeout(ctx, whatsappLogoutTime)
494 logoutErr = client.Logout(ctx)
497 err = w.removeSession()
499 answer("The unlink failed: " + w.redact(err.Error()))
503 err = l.note("WhatsApp is unlinked.")
505 w.logger.Printf("note the unlink: %v", err)
508 if client == nil || logoutErr != nil {
509 answer("WhatsApp is unlinked here, but the phone did not get the logout. In WhatsApp on your phone, open Linked devices and remove the device.")
513 answer("WhatsApp is unlinked.")
518 // removeSession ends the connection, closes the session database, and deletes
520 func (w *WhatsApp) removeSession() error {
532 container := w.container
537 w.inbox.stopCatchup()
544 if container != nil {
545 err = container.Close()
548 for _, suffix := range []string{"", "-wal", "-shm"} {
549 removeErr := os.Remove(w.sessionPath() + suffix)
550 if !errors.Is(removeErr, os.ErrNotExist) {
551 err = errors.Join(err, removeErr)
558 // pair links a new device to the account of phone with a pairing code. It gives
559 // the steps and the code to answer, and then the end of the pairing: the linked
560 // number, an expired code, or a failure. A pairing that a new one ends gives no
561 // answer. A link notes "WhatsApp is linked." in the log of l, with no number.
562 // The client of a pairing that does not link is disconnected.
564 // ctx limits the pairing, but the connection uses runCtx, the context of Run.
565 // The library keeps the context of the connection for its keep-alive, its event
566 // handler, and its reconnections, and after the link the server ends the stream
567 // and the library connects again. With ctx, the end of the pairing would stop
568 // that reconnection, and the linked device would never connect.
569 func (w *WhatsApp) pair(ctx, runCtx context.Context, l *TurnLoop, phone string, answer func(text string)) {
570 device, err := w.newDevice(ctx)
572 answer("The pairing failed: " + err.Error())
576 client := w.newClient(device, w.handle)
584 qr, err := client.GetQRChannel(ctx)
586 err = client.ConnectContext(runCtx)
590 answer("The connection to WhatsApp failed: " + err.Error())
595 case item, open := <-qr:
596 if !open || item.Event != whatsmeow.QRChannelEventCode {
597 answer("WhatsApp did not start the pairing. Try again later.")
601 if errors.Is(ctx.Err(), context.DeadlineExceeded) {
602 answer("The connection to WhatsApp did not start the pairing in time. Try again later.")
608 code, err := client.PairPhone(ctx, phone, true, whatsmeow.PairClientChrome, "Chrome (Linux)")
610 answer("WhatsApp refused the pairing: " + err.Error() + ". Check the phone number and run /whatsapp pair PHONE again.")
614 answer(whatsappPairSteps)
618 var item whatsmeow.QRChannelItem
621 case item, open = <-qr:
623 if errors.Is(ctx.Err(), context.DeadlineExceeded) {
624 answer(whatsappPairExpired)
631 case !open || item.Event == whatsmeow.QRChannelTimeout.Event:
632 answer(whatsappPairExpired)
634 case item.Event == whatsmeow.QRChannelEventCode:
636 case item.Event == whatsmeow.QRChannelSuccess.Event:
640 err = l.note("WhatsApp is linked.")
642 w.logger.Printf("note the link: %v", err)
645 answer("WhatsApp is linked to +" + phone + ".")
649 answer("The pairing failed: " + whatsappItemError(item) + ". Run /whatsapp pair PHONE again.")
655 // newDevice returns a new device in the session database. It makes the
656 // directory and the file of the session first, with their modes, when they do
658 func (w *WhatsApp) newDevice(ctx context.Context) (*store.Device, error) {
660 defer w.mutex.Unlock()
662 if w.container == nil {
663 err := os.MkdirAll(w.dir, 0o700)
665 return nil, fmt.Errorf("create WhatsApp directory: %w", err)
668 file, err := os.OpenFile(w.sessionPath(), os.O_CREATE|os.O_RDWR, 0o600)
670 return nil, fmt.Errorf("create WhatsApp session: %w", err)
675 return nil, fmt.Errorf("create WhatsApp session: %w", err)
678 err = w.openSession(ctx)
684 w.device = w.container.NewDevice()
689 // linkedBy makes client the client of the linked device.
690 func (w *WhatsApp) linkedBy(client WhatsAppClient) {
692 defer w.mutex.Unlock()
698 // handle takes one event of the library and tells if it was handled. A message
699 // goes to the inbox. The names of the groups follow the events of the groups. A
700 // connection starts a catch-up of the inbox, which ends at OfflineSyncCompleted
701 // or after its catchupLimit. A disconnection keeps the time of its start while
702 // the library tries again. After noticeDelay of a disconnection, all clients
703 // get one notice, and they get one more when the connection comes back. A
704 // disconnection that the library does not try again gives a notice at once.
705 func (w *WhatsApp) handle(event any) bool {
706 switch event := event.(type) {
707 case *events.Message:
708 return w.inbox.receive(w.link(), event)
709 case *events.GroupInfo:
710 if event.Name != nil {
711 w.groups.set(event.JID.String(), event.Name.Name)
715 case *events.JoinedGroup:
716 w.groups.set(event.JID.String(), event.Name)
718 case *events.OfflineSyncCompleted:
721 case *events.Connected:
722 w.inbox.startCatchup()
727 switch event := event.(type) {
728 case *events.PairSuccess:
729 w.own = whatsappOwnIDs(event.ID.User, event.LID.User)
730 case *events.Connected:
732 w.connectedAt = w.clock()
739 notice = "WhatsApp is connected again."
741 case *events.Disconnected:
742 if w.linked && (w.connected || w.disconnectedAt.IsZero()) {
743 w.disconnectedAt = w.clock()
745 disconnections := w.disconnections
746 w.timer = time.AfterFunc(w.noticeDelay, func() {
747 w.disconnectedLong(disconnections)
752 case *events.LoggedOut:
754 case w.logouts <- struct{}{}:
757 case events.PermanentDisconnect:
761 notice = "WhatsApp is disconnected and does not connect again by itself: " + event.PermanentDisconnectDescription() + ". Restart the daemon to try again."
773 // disconnectedLong gives the notice of a long disconnection, unless the
774 // disconnection with the number disconnections ended.
775 func (w *WhatsApp) disconnectedLong(disconnections int) {
777 long := !w.closed && w.linked && !w.connected && !w.noticed && w.disconnections == disconnections
782 since := w.disconnectedAt
786 w.broadcast("WhatsApp is disconnected since " + since.Format(whatsappTimeLayout) + ". OneChat tries again.")
790 // broadcast gives text as a notice to all clients.
791 func (w *WhatsApp) broadcast(text string) {
797 if notify != nil && !closed {
805 // redact replaces the own number and the own LID in text with OWN.
806 func (w *WhatsApp) redact(text string) string {
808 defer w.mutex.Unlock()
810 return whatsappRedact(w.own, text)
813 // Send sends text to chat, the ID of a private chat or a group, and returns the
814 // answer of the tool. id names the send: a call with an id that has a record
815 // gives the answer of the record and sends nothing, and the same id with
816 // another chat or text is an error. The record "sending" is durable before the
817 // send, so a stop of the daemon during the send gives an answer that the result
818 // is unknown, and not a second send. A disconnected WhatsApp sends nothing and
820 func (w *WhatsApp) Send(ctx context.Context, id, chat, text string) (string, error) {
825 connected := w.connected
826 own := slices.Clone(w.own)
830 return whatsappNotLinked, nil
833 jid, err := whatsappSendCheck(id, chat, text, own)
838 digest := sha256.Sum256([]byte(text))
839 record := WhatsAppSend{
842 Digest: hex.EncodeToString(digest[:]),
843 State: whatsappSending,
847 held, found := w.state.Send(id)
849 case found && (held.Chat != record.Chat || held.Digest != record.Digest):
850 return "", fmt.Errorf("the id %s belongs to another message: give a new id for each message", id)
851 case found && held.State == whatsappSending:
852 return "The send with this id started, but its result is unknown, because the daemon stopped during it. Ask the user to look at the chat in WhatsApp before you send the message again with a new id.", nil
854 return held.Answer + " This is the answer of the first call with this id: nothing was sent again.", nil
855 case client == nil || !connected:
856 return "", errors.New("WhatsApp is disconnected, so nothing was sent: try again later")
859 record.MessageID = client.GenerateMessageID()
860 err = w.state.PutSend(record)
865 sendCtx, cancel := context.WithTimeout(ctx, whatsappSendTime)
868 response, err := client.SendMessage(sendCtx, jid, &waE2E.Message{
869 Conversation: new(text),
870 }, whatsmeow.SendRequestExtra{
871 ID: record.MessageID,
874 record.State = whatsappSent
876 record.State = whatsappFailed
877 record.Answer = "The send failed: " + w.redact(err.Error()) + "."
879 name := w.groups.name(ctx, w.link(), jid.String())
880 if jid.Server != types.GroupServer {
881 name = whatsappContactName(ctx, device, jid, "")
884 record.Answer = "Sent to " + name + " at " + response.Timestamp.In(w.clock().Location()).Format("15:04") + "."
887 err = w.state.PutSend(record)
892 return record.Answer, nil
895 // WhatsAppLink is a copy of the link of the device at one time, for the parts
896 // of WhatsApp that do not own it: the groups, the batches, find, and send.
897 type WhatsAppLink struct {
900 client WhatsAppClient
905 // WhatsAppRequest is a pair or unlink command for Run, with the function that
906 // gives its answers to the client that asked. A pairing has the phone number.
907 type WhatsAppRequest struct {
910 answer func(text string)
913 // whatsappNewClient returns the client of the library for device, with handle
914 // as its event handler. The client tries again after a failed first connection,
915 // acknowledges a message only after handle returns true, and downloads no old
916 // history. The phone shows the device with the name OneChat.
917 func whatsappNewClient(device *store.Device, handle func(event any) bool) WhatsAppClient {
918 store.SetOSInfo("OneChat", [3]uint32{0, 1, 0})
919 client := whatsmeow.NewClient(device, waLog.Noop)
920 client.InitialAutoReconnect = true
921 client.SynchronousAck = true
922 client.ManualHistorySyncDownload = true
923 client.AddEventHandlerWithSuccessStatus(handle)
928 // whatsappContactName returns the name of the person jid: the full name, the
929 // first name, the business name, or the push name of the contact in the session
930 // of device, then push, the push name of a message, then the phone number. A
931 // LID gets its phone number from the LID map of the session.
932 func whatsappContactName(ctx context.Context, device *store.Device, jid types.JID, push string) string {
934 candidates := []types.JID{jid}
935 phone := types.EmptyJID
937 case types.DefaultUserServer:
939 case types.HiddenUserServer:
940 mapped, err := device.LIDs.GetPNForLID(ctx, jid)
941 if err == nil && !mapped.IsEmpty() {
943 candidates = append(candidates, mapped)
947 for _, candidate := range candidates {
948 contact, err := device.Contacts.GetContact(ctx, candidate)
949 if err != nil || !contact.Found {
953 for _, name := range []string{contact.FullName, contact.FirstName, contact.BusinessName, contact.PushName} {
963 case !phone.IsEmpty():
964 return "+" + phone.User
970 // whatsappSendCheck returns the JID of chat, and an error when id, chat, or
971 // text cannot go to a send. The chat is a private chat or a group, and not the
972 // chat of the user with itself.
973 func whatsappSendCheck(id, chat, text string, own []string) (types.JID, error) {
974 jid, err := types.ParseJID(chat)
976 case strings.TrimSpace(id) == "" || len(id) > whatsappSendIDBytes:
977 return types.EmptyJID, fmt.Errorf("the id must have 1 to %d bytes", whatsappSendIDBytes)
978 case strings.TrimSpace(text) == "" || len(text) > whatsappSendBytes:
979 return types.EmptyJID, fmt.Errorf("the text must have 1 to %d bytes", whatsappSendBytes)
980 case err != nil || jid.User == "" || jid != jid.ToNonAD() || jid.String() != chat:
981 return types.EmptyJID, fmt.Errorf("%q is not the ID of a WhatsApp chat: find the ID with whatsapp_find", chat)
982 case jid.Server != types.DefaultUserServer && jid.Server != types.HiddenUserServer && jid.Server != types.GroupServer:
983 return types.EmptyJID, fmt.Errorf("%q is not a private chat or a group", chat)
984 case slices.Contains(own, jid.User):
985 return types.EmptyJID, errors.New("this is the chat of the user with itself: send to another chat")
991 // whatsappOwnIDs returns the own number and the own LID that are not empty.
992 func whatsappOwnIDs(ids ...string) []string {
994 for _, id := range ids {
996 own = append(own, id)
1003 // whatsappRedact replaces each of the own IDs own in text with OWN.
1004 func whatsappRedact(own []string, text string) string {
1005 for _, id := range own {
1006 text = strings.ReplaceAll(text, id, "OWN")
1012 // whatsappSessionAddress returns the SQLite address of the session database at
1013 // path, with foreign keys on, as the library needs, and a wait for a busy
1015 func whatsappSessionAddress(path string) (string, error) {
1016 path, err := filepath.Abs(path)
1018 return "", fmt.Errorf("find WhatsApp session: %w", err)
1024 RawQuery: "_pragma=foreign_keys(1)&_pragma=busy_timeout(5000)",
1027 return address.String(), nil
1030 // whatsappCommandArgs returns the text after /whatsapp of a line of a WhatsApp
1031 // command, or false for any other line.
1032 func whatsappCommandArgs(line string) (string, bool) {
1033 line = strings.TrimSpace(line)
1034 if line == whatsappCommand {
1038 args, found := strings.CutPrefix(line, whatsappCommand+" ")
1043 return strings.TrimSpace(args), true
1046 // whatsappPhone returns the digits of phone, a number with its country code and
1047 // an optional + and dashes, and tells if it has the form of a number: 7 to 15
1048 // digits, with no 0 first.
1049 func whatsappPhone(phone string) (string, bool) {
1050 phone = strings.ReplaceAll(strings.TrimPrefix(phone, "+"), "-", "")
1051 valid := len(phone) >= 7 && len(phone) <= 15 && phone[0] != '0' && strings.Trim(phone, "0123456789") == ""
1056 // whatsappItemError returns the error of a pairing item that is not a code, a
1057 // success, or a timeout.
1058 func whatsappItemError(item whatsmeow.QRChannelItem) string {
1059 if item.Error != nil {
1060 return item.Error.Error()