rohitsangwan01 / whatsapp_bot_flutter

Whatsapp bot for flutter desktop
MIT License
40 stars 26 forks source link

How to login from mobile? #48

Closed adifyr closed 7 months ago

adifyr commented 1 year ago

Hi. I'm unable to use the plugin on mobile because I can't login.

My WhatsAppClient is null. What do I do? Here is my code so far:

void _sendMessage() async {
  setState(() => _processing = 'Initializing WhatsApp Chatbot');

  final client = await WhatsappBotFlutterMobile.connect(
    saveSession: true,
    onConnectionEvent: (event) => debugPrintSynchronously('Connection Event: ${event.name}'),
    onQrCode: (qrCodeUrl, qrCodeImage) {
      debugPrintSynchronously('Connection QR Code URL: $qrCodeUrl');
    },
  ).catchError((error) {
    debugPrintSynchronously('There was an error initializing the whatsapp client: $error');
    return null;
  });

  if (client == null) {
    ScaffoldMessenger.of(context).showSnackBar(const SnackBar(
      backgroundColor: Clr.red,
      content: Text('WhatsApp Connection Failed. Client is null'),
    ));
    setState(() => _processing = null);
    return;
  }

  setState(() => _processing = 'Sending Message');
  if (_text.isNotEmpty) await client.chat.sendTextMessage(phone: '918892100457', message: _text);
}
adifyr commented 1 year ago

Can you show a complete example on how to do it on mobile? From Login to Send Message?

rohitsangwan01 commented 1 year ago

@adifyr to login from the same mobile , you can print the qrCode in terminal using

onQrCode: (qrCodeUrl, qrCodeImage) {
     print(WhatsappBotUtils.convertStringToQrCode(qrCodeImage));
 },

and scan that with whatsapp app this is for debugging only, but i don't think if we can scan the qrCode within mobile from whatsapp