Skip to content

Commit

Permalink
[ADD] Added descriptions to README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
Jaesung Lee committed May 15, 2023
1 parent 8b31062 commit c20e02a
Show file tree
Hide file tree
Showing 5 changed files with 159 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,9 @@ class ChatGPTChannelListViewController: SBUGroupChannelListViewController, BotTy
self.navigationController?.view.addSubview(createChannelTypeSelector)
}

func botTypeSelector(_ botTypeSelect: BotTypeSelector, didSelect bot: ChatBot) {
func botTypeSelector(_ botTypeSelect: BotTypeSelector, didSelectBot bot: ChatBot) {
botTypeSelect.dismiss()
createChannelViewModel?.createChannel(userIds: [bot.botID])

}

override func baseChannelListModule(_ headerComponent: SBUBaseChannelListModule.Header, didTapRightItem rightItem: UIBarButtonItem) {
Expand Down
10 changes: 5 additions & 5 deletions ChatGPTExample/Views/BotTypeSelector.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import UIKit
import SendbirdUIKit

/// Please refer to ``ChatGPTChannelListViewController/botTypeSelector(_:didSelect:)``
protocol BotTypeSelectorDelegate: SBUCreateChannelTypeSelectorDelegate {
func botTypeSelector(_ botTypeSelect: BotTypeSelector, didSelect bot: ChatBot)
protocol BotTypeSelectorDelegate: AnyObject {
func botTypeSelector(_ botTypeSelect: BotTypeSelector, didSelectBot bot: ChatBot)
}

/// Please refer to ``ChatGPTChannelListViewController/loadChannelTypeSelector()``
Expand Down Expand Up @@ -77,15 +77,15 @@ class BotTypeSelector: SBUCreateChannelTypeSelector {

@objc
func onTapChatGPTBot() {
self.botSelectorDelegate?.botTypeSelector(self, didSelect: .chatGPT)
self.botSelectorDelegate?.botTypeSelector(self, didSelectBot: .chatGPT)
}

@objc func onTapWittyBot() {
botSelectorDelegate?.botTypeSelector(self, didSelect: .witty)
botSelectorDelegate?.botTypeSelector(self, didSelectBot: .witty)
}

@objc func onTapKnowledgeBot() {
botSelectorDelegate?.botTypeSelector(self, didSelect: .knowledge)
botSelectorDelegate?.botTypeSelector(self, didSelectBot: .knowledge)
}


Expand Down
155 changes: 153 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,153 @@
# sendbird-chatgpt-sample-ios
GPT Sample in iOS
# Sendbird ChatGPT Sample for iOS

This is an example of [Sendbird ChatGPT](https://sendbird.com/docs/chat/v3/platform-api/bot/bot-overview#1-overview) for iOS, implemented using [Sendbird UIKit](https://sendbird.com/docs/uikit/v3/ios/overview).

<p>
Sendbird ChatGPT: Sendbird ChatGPT is a Sendbird Chat integration into your existing ChatGPT bot can provide customers with a more engaging and personalized conversation experience.
</p>

<p align="center">
<img
src="./channel_list.png"
width="250"
/>
<img
src="./channel.png"
width="250"
/>
</p>

## Getting Started
1. Create your Sendbird application on [the dashboard](https://dashboard.sendbird.com/auth/signup).
2. [Register the ChatGPT bot](https://sendbird.com/developer/tutorials/chatbot-google-dialogflow) in your Sendbird application.
3. In this example, we're connecting to a test purpose Sendbird application and pre-defined bots. To connect yours, replace `applicationId` and others in the `AppDelegate.swift` file and replace `ChatBot` in the `ChatBot.swift` file as follows:

```swift
// AppDelegate.swift
@main
class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.

// TODO: Replace with your own APP_ID
SendbirdUI.initialize(applicationId: "13B6D179-33A5-4C0D-9162-E11DAC9358FC") { error in
//
}

SBUGlobals.currentUser = SBUUser(userId: "j_sung_0o0")

// ...
}

// ...
}
```

```swift
// ChatBot.swift
enum ChatBot: Int, CaseIterable {
// TODO: Replace with your own user IDs of bots
case chatGPT

var botID: String {
switch self {
case .chatGPT: return "gpt_bot"
}
}
```

4. Build & Run

## How to connect a channel to a bot
This sample supports chat with 3 different bots. They are trained with 3 different characteristics.
Currently, Only 1:1 chat with ChatGPT bot is supoorted.When you create a channel, you just need to enter the user ID of the bot you want to chat with.

```swift
createChannelViewModel?.createChannel(userIds: [ChatBot.chatGPT.botID])
```

For more information, see [our documentation](https://sendbird.com/docs/chat/v3/platform-api/bot/bot-overview#1-overview).

## Implementation

### Bot type selector

```swift
// 1
class BotTypeSelector: SBUCreateChannelTypeSelector {
// 3
weak var botSelectorDelegate: BotTypeSelectorDelegate?

// 4
@objc
func onTapChatGPTBot() {
self.botSelectorDelegate?.botTypeSelector(self, didSelectBot: .chatGPT)
}
}

// 2
protocol BotTypeSelectorDelegate: AnyObject {
func botTypeSelector(_ botTypeSelect: BotTypeSelector, didSelectBot bot: ChatBot)
}
```
1. Override `SBUCreateChannelTypeSelector` to use same design.
2. Define protocol called `BotTypeSelectorDelegate` as event delegate
3. Declare delegate property as weak referenced.
4. Call delegate method when the bot button selected.

### Channel list - SBUCreateChannelViewModel
```swift
class ChatGPTChannelListViewController: SBUGroupChannelListViewController, SBUCreateChannelViewModelDelegate {
var createChannelViewModel: SBUCreateChannelViewModel?

// 1
override func createViewModel(channelListQuery: GroupChannelListQuery?) {
super.createViewModel(channelListQuery: channelListQuery)
self.createChannelViewModel = SBUCreateChannelViewModel(delegate: self)
}

// 2
func createChannelViewModel(_ viewModel: SBUCreateChannelViewModel, didCreateChannel channel: BaseChannel?, withMessageListParams messageListParams: MessageListParams?) {
guard let channelURL = channel?.channelURL else { return }
SendbirdUI.moveToChannel(channelURL: channelURL, messageListParams: messageListParams)
}
}
```
1. Create `SBUCreateChannelViewModel` instance after `super.createViewModel(channelListQuery:)` is called.
2. When `didCreateChannel` is called, call `SendbirdUI.moveToChannel(channelURL:messageListParams:)`

### Channel list - BotTypeSelector

```swift
extension ChatGPTChannelListViewController: BotTypeSelectorDelegate {
// 1
override func loadChannelTypeSelector() {
if self.createChannelTypeSelector == nil {
let botTypeSelector = BotTypeSelector(delegate: self)
botTypeSelector.botSelectorDelegate = self
botTypeSelector.isHidden = true
self.createChannelTypeSelector = botTypeSelector
}

guard let createChannelTypeSelector else { return }
self.navigationController?.view.addSubview(createChannelTypeSelector)
}

// 2
override func baseChannelListModule(_ headerComponent: SBUBaseChannelListModule.Header, didTapRightItem rightItem: UIBarButtonItem) {
self.showCreateChannelTypeSelector()
}

// 3
func botTypeSelector(_ botTypeSelect: BotTypeSelector, didSelectBot bot: ChatBot) {
botTypeSelect.dismiss()
createChannelViewModel?.createChannel(userIds: [bot.botID])
}
}
```
1. Override `loadChannelTypeSelector` to use `BotTypeSelector`.
2. Override `didTapRightItem` delegate method to call `showCreateChannelTypeSelector()`. This will show `BotTypeSelector`.
3. When bot type is selected from `BotTypeSelector`, `botTypeSelector(_:didSelectBot:)` delegate method is called. In the method, call `SBUCreateChannelViewModel/createChannel(userIds:)`.
Binary file added channel.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added channel_list.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit c20e02a

Please sign in to comment.