rohit-chouhan / whatsapp

A package for whatsapp business api, send messages, product, media, location through flutter app.
https://pub.dev/packages/whatsapp
MIT License
24 stars 18 forks source link

How to receive whatsapp messages? #18

Open narendran1996 opened 2 months ago

narendran1996 commented 2 months ago

I need to create a application inside that i have a chat option to send and receive messages. In this plugin documentation i could not able to see receive message option.

ismailme33 commented 3 weeks ago

I need to create a application inside that i have a chat option to send and receive messages. In this plugin documentation i could not able to see receive message option.

Hi Dear

Could you please help me on this? getting this error

Restarted application in 453ms. flutter: HTTP Code: null flutter: API Error: null flutter: Request Error: ClientException: Failed to parse header value flutter: API Response: null

my code,

import 'package:flutter/material.dart';
import 'package:whatsapp/whatsapp.dart';

void main() => runApp(MaterialApp(home: Home()));

class Home extends StatefulWidget {
  const Home({super.key});

  @override
  State<Home> createState() => _HomeState();
}

class _HomeState extends State<Home> {
  late WhatsApp whatsapp; // Declare as late to initialize later

  final String accessToken1 =
      'myaccesstoken';
  final String fromNumberId = 'myphonenumberid';

  @override
  void initState() {
    super.initState();

    // Initialize WhatsApp instance here
    whatsapp = WhatsApp(accessToken1, fromNumberId);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: ListView(
        children: [
          TextButton(
            onPressed: () async {
              if (whatsapp != null) {
                try {
                  var res = await whatsapp.sendMessage(
                    phoneNumber: '+919019.....',
                    text: 'Hello from Flutter!',
                    previewUrl: true,
                  );

                  if (res.isSuccess()) {
                    debugPrint('Message ID: ${res.getMessageId()}');
                    debugPrint('Message sent to: ${res.getPhoneNumber()}');
                    debugPrint('API Response: ${res.getResponse().toString()}');
                  } else {
                    debugPrint('HTTP Code: ${res.getHttpCode()}');
                    debugPrint('API Error: ${res.getErrorMessage()}');
                    debugPrint('Request Error: ${res.getError()}');
                    debugPrint('API Response: ${res.getResponse().toString()}');
                  }
                } catch (e) {
                  debugPrint('Error: $e');
                }
              } else {
                debugPrint('WhatsApp is not initialized!');
              }
            },
            child: const Text("Send Message"),
          ),
        ],
      ),
    );
  }
}

![Uploading image.png…]()

narendran1996 commented 3 weeks ago

@ismailme33 While sending the message are u getting this issue ?

ismailme33 commented 1 week ago

@narendran1996 yes, while sending message