bingchat_api

package module
v1.0.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 13, 2023 License: MIT Imports: 13 Imported by: 0

README

Bing Chat

Requirements
  • Install Cookie-Editor in your browser.
  • Export bing.com cookies with json
Warn

This project currently support parallel sessions (multi sessions) and context

Please make sure that websocket be closed at the end of conversation

Use
go get github.com/NoahAmethyst/bingchat-api
Example

You can see example codes in chat_test.go which include conversation with context and multi conversations

Test

import (
"fmt"
bingchat_api "github.com/NoahAmethyst/bingchat-api"
"os"
"strings"
"testing"
"time"
)

func Test_Conversation(t *testing.T) {
    chat, err := bingchat_api.NewBingChat(os.Getenv("COOKIE"), bingchat_api.ConversationBalanceStyle, 2*time.Minute)
    if err != nil {
        panic(err)
    }

    defer func() {
        chat.Close()
        t.Logf("%+v", chat.CheckAlive())
    }()
	
    message, err := chat.SendMessage("how is the weather today in Seattle")
    if err != nil {
        panic(err)
    }
	
    var respBuilder strings.Builder
    for {
        msg, ok := <-message.Notify
        if !ok {
            fmt.Println()
            break
        }
        respBuilder.WriteString(msg)
    }

    t.Logf("%s", respBuilder.String())

}

Documentation

Index

Constants

View Source
const (
	DELIMITER = "\x1e"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type BingChatHub

type BingChatHub struct {
	sync.Mutex
	// contains filtered or unexported fields
}

func (*BingChatHub) CheckAlive

func (b *BingChatHub) CheckAlive() bool

func (*BingChatHub) Close

func (b *BingChatHub) Close()

Close the websocket collection with new bing chat

func (*BingChatHub) Reset

func (b *BingChatHub) Reset(style ...ConversationStyle)

Reset conversation style,the supported style list is: ConversationCreateStyle ConversationBalanceStyle ConversationPreciseStyle

func (*BingChatHub) SendMessage

func (b *BingChatHub) SendMessage(msg string) (*MsgResp, error)

SendMessage send message to bing chat and return a response with message(string) channel which you should receive the element from channel to get truly response message

func (*BingChatHub) SetRemote added in v1.0.1

func (b *BingChatHub) SetRemote(url, ws string)

SetRemote can set your own url & ws which available with new bing chat

func (*BingChatHub) Style

func (b *BingChatHub) Style() ConversationStyle

Style return current conversation style

type ConversationSession

type ConversationSession struct {
	ConversationID        string `json:"conversationId"`
	ClientID              string `json:"clientId"`
	ConversationSignature string `json:"conversationSignature"`
	Result                struct {
		Value   string      `json:"value"`
		Message interface{} `json:"message"`
	} `json:"result"`
}

type ConversationStyle

type ConversationStyle uint8
const (
	ConversationCreateStyle ConversationStyle = iota + 1
	ConversationBalanceStyle
	ConversationPreciseStyle
)

func (ConversationStyle) String

func (c ConversationStyle) String() string

func (ConversationStyle) TmpMessage

func (c ConversationStyle) TmpMessage() *SendMessage

type HandShake

type HandShake struct {
	Protocol string `json:"protocol"`
	Version  int    `json:"version"`
}

type IBingChat

type IBingChat interface {
	Reset(style ...ConversationStyle)
	SendMessage(msg string) (*MsgResp, error)
	Style() ConversationStyle
	Close()
	CheckAlive() bool
	SetRemote(url, ws string)
}

func NewBingChat

func NewBingChat(cookiesJson string, style ConversationStyle, timeout time.Duration) (IBingChat, error)

type MessageResp

type MessageResp struct {
	Type   int    `json:"type"`
	Target string `json:"target"`
	Item   struct {
		Messages []struct {
			MessageType string `json:"messageType,omitempty"`
		} `json:"messages"`
	} `json:"item"`
	Arguments []struct {
		Cursor struct {
			J string `json:"j"`
			P int    `json:"p"`
		} `json:"cursor"`
		Messages []struct {
			Text          string    `json:"text"`
			Author        string    `json:"author"`
			CreatedAt     time.Time `json:"createdAt"`
			Timestamp     time.Time `json:"timestamp"`
			MessageId     string    `json:"messageId"`
			Offense       string    `json:"offense"`
			MessageType   string    `json:"messageType"`
			AdaptiveCards []struct {
				Type    string `json:"type"`
				Version string `json:"version"`
				Body    []struct {
					Type string `json:"type"`
					Text string `json:"text"`
					Wrap bool   `json:"wrap"`
					Size string `json:"size"`
				} `json:"body"`
			} `json:"adaptiveCards"`
			SourceAttributions []SourceAttribution `json:"sourceAttributions"`
			Feedback           struct {
				Tag       interface{} `json:"tag"`
				UpdatedOn interface{} `json:"updatedOn"`
				Type      string      `json:"type"`
			} `json:"feedback"`
			ContentOrigin      string      `json:"contentOrigin"`
			Privacy            interface{} `json:"privacy"`
			SuggestedResponses []struct {
				Text        string    `json:"text"`
				Author      string    `json:"author"`
				CreatedAt   time.Time `json:"createdAt"`
				Timestamp   time.Time `json:"timestamp"`
				MessageId   string    `json:"messageId"`
				MessageType string    `json:"messageType"`
				Offense     string    `json:"offense"`
				Feedback    struct {
					Tag       interface{} `json:"tag"`
					UpdatedOn interface{} `json:"updatedOn"`
					Type      string      `json:"type"`
				} `json:"feedback"`
				ContentOrigin string      `json:"contentOrigin"`
				Privacy       interface{} `json:"privacy"`
			} `json:"suggestedResponses"`
		} `json:"messages"`
		RequestId string `json:"requestId"`
	} `json:"arguments"`
}

type MsgResp

type MsgResp struct {
	Suggest    []string
	Notify     chan string
	Title      string
	References map[string]string
}

type SendMessage

type SendMessage struct {
	Arguments []struct {
		Source              string   `json:"source"`
		OptionsSets         []string `json:"optionsSets"`
		AllowedMessageTypes []string `json:"allowedMessageTypes"`
		SliceIds            []string `json:"sliceIds"`
		Verbosity           string   `json:"verbosity"`
		TraceId             string   `json:"traceId"`
		IsStartOfSession    bool     `json:"isStartOfSession"`
		Message             struct {
			Locale        string `json:"locale"`
			Market        string `json:"market"`
			Region        string `json:"region"`
			Location      string `json:"location"`
			LocationHints []struct {
				Country           string `json:"country"`
				Timezoneoffset    int    `json:"timezoneoffset"`
				CountryConfidence int    `json:"countryConfidence"`
				Center            struct {
					Latitude  float64 `json:"Latitude"`
					Longitude float64 `json:"Longitude"`
				} `json:"Center"`
				RegionType int `json:"RegionType"`
				SourceType int `json:"SourceType"`
			} `json:"locationHints"`
			Timestamp   time.Time `json:"timestamp"`
			Author      string    `json:"author"`
			InputMethod string    `json:"inputMethod"`
			Text        string    `json:"text"`
			MessageType string    `json:"messageType"`
		} `json:"message"`
		ConversationSignature string `json:"conversationSignature"`
		Participant           struct {
			Id string `json:"id"`
		} `json:"participant"`
		ConversationId string `json:"conversationId"`
	} `json:"arguments"`
	InvocationId string `json:"invocationId"`
	Target       string `json:"target"`
	Type         int    `json:"type"`
}

type SourceAttribution added in v1.0.3

type SourceAttribution struct {
	ProviderDisplayName string `json:"providerDisplayName"`
	SeeMoreUrl          string `json:"seeMoreUrl"`
	SearchQuery         string `json:"searchQuery"`
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL