bingchat

package
v0.0.0-...-02b0d32 Latest Latest
Warning

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

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

README

Bing Chat

Requirements
  • Install Cookie-Editor in your browser.
  • Export bing.com cookies with json
Use
go get github.com/agreyfox/bardapi/bingchat
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)
    }
	
    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

CheckAlive check whether websocket collection is alive

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 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) 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
}

func NewBingChat

func NewBingChat(cookiesJsonFile 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 {
			Text          string `json:"text,omitempty"`
			HiddenText    string `json:"hiddenText,omitempty"`
			Author        string `json:"author,omitempty"`
			MessageType   string `json:"messageType,omitempty"`
			AdaptiveCards []struct {
				Body []struct {
					Type string `json:"type"`
					Text string `json:"text"`
					Wrap string `json:"wrap"`
				}
			} `json:"adaptiveCards,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 []interface{} `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
	Source  []string //TODO
}

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"`
}

Jump to

Keyboard shortcuts

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