contigo_chat 0.0.2 copy "contigo_chat: ^0.0.2" to clipboard
contigo_chat: ^0.0.2 copied to clipboard

Dart package to connect to Odoo Discuss Module

example/contigo_chat_example.dart

import 'package:contigo_chat/contigo_chat.dart';
import 'package:flutter/foundation.dart';

Future<void> main(List<String> args) async {
  // Init
  final contigoChat = ContigoChat(
    serverUrl: 'http://localhost:8069',
    databaseName: 'acme',
    debug: true,
  );

  // Login
  final loginResult = await contigoChat.login(
    username: 'youruser@odoo.com',
    password: '12345678',
  );

  if (kDebugMode) {
    print(loginResult);
  }

  // Init messaging
  final messagingResult = await contigoChat.initMessaging();

  if (kDebugMode) {
    print('Current Partner: ');

    print(messagingResult.currentPartner);

    print('Channels, Private Messages, Groups: ');
    print(messagingResult.channels);
  }
  if (messagingResult.channels.isEmpty) {
    if (kDebugMode) {
      print('No channels found');
    }
    return;
  }

  // Fetch messages
  final messages = await contigoChat.fetchMessages(
    messagingResult.channels.first.id,
  );

  if (kDebugMode) {
    print('Messages: ');
  }
  for (final message in messages.reversed) {
    if (kDebugMode) {
      print(message.emailFrom);
      print(message.author);
      print(message.body);
      print(message.date);
      print('\n');
    }
  }

  // Send message
  final newMessageId = await contigoChat.sendMessage(
    channelId: messagingResult.channels.first.id,
    message: 'Test message from ContigoChat Flutter',
  );

  if (kDebugMode) {
    print('New message id: $newMessageId');
  }

  // Poll
  while (await Future<bool>.delayed(const Duration(seconds: 3), () => true)) {
    final results = await contigoChat.poll();

    for (final result in results) {
      if (kDebugMode) {
        print('\n');
      }
      switch (result.message) {
        case PollMessageMessage(data: final Message data):
          if (kDebugMode) {
            print('Is a message');
            print(data);
          }
        case PollMessageChannel(data: final Channel data):
          if (kDebugMode) {
            print('Is a new channel notification');
            print(data);
          }
        case PollMessageInfo(data: final MessageInfo data):
          if (kDebugMode) {
            print('Is an info message');
          } // ex: typing, or bot messages
          switch (data) {
            case MessageInfoTyping(
                isTyping: final bool isTyping,
                partnerId: final int partnerId,
                partnerName: final String partnerName,
              ):
              if (kDebugMode) {
                print('Partner id: $partnerId');
                print('Partner name: $partnerName');
                print('Is typing: $isTyping');
              }

            case MessageInfoTransient(
                body: final String body,
              ):
              if (kDebugMode) {
                print('Transient message: $body');
              }
          }
        case null:
          if (kDebugMode) {
            print('Empty poll result');
          }
      }
    }
  }
}
0
likes
70
points
58
downloads

Publisher

unverified uploader

Weekly Downloads

Dart package to connect to Odoo Discuss Module

Homepage

Topics

#odoo #chat #messaging

Documentation

API reference

License

MIT (license)

Dependencies

cookie_jar, dio, dio_cookie_manager, equatable, flutter, freezed_annotation, json_annotation, retrofit

More

Packages that depend on contigo_chat