This is sample code that uses the filteredstream package.
Suppose you want to retrieve streams filtered by the keyword "Twitter API v2", create a search stream rule with the following code.
func CreateSearchStreanmRules(c *gotwi.Client) {
p := &types.CreateRulesInput{
Add: []types.AddingRule{
{Value: gotwi.String("Twitter API v2"), Tag: gotwi.String("example rule")},
},
}
res, err := filteredstream.CreateRules(context.TODO(), c, p)
if err != nil {
fmt.Println(err.Error())
return
}
for _, r := range res.Data {
fmt.Printf("ID: %s, Value: %s, Tag: %s\n", gotwi.StringValue(r.ID), gotwi.StringValue(r.Value), gotwi.StringValue(r.Tag))
}
}To retrieve the streams to which you have applied the rules (filters) you have created, implement as follows.
func SearchStream(c *gotwi.Client) {
p := &types.SearchStreamInput{}
s, err := filteredstream.SearchStream(context.Background(), c, p)
if err != nil {
fmt.Println(err)
return
}
cnt := 0
for s.Receive() {
cnt++
t, err := s.Read()
if err != nil {
fmt.Println(err)
} else {
fmt.Println(gotwi.StringValue(t.Data.ID), gotwi.StringValue(t.Data.Text))
}
if cnt > 10 {
s.Stop()
break
}
}
}If you want to adjust the time to run the stream, pass http.Client with an arbitrary timeout as input when generating gotwi.Client as shown below. (default is 30 seconds)
For example 120 sec.
func main() {
in := &gotwi.NewClientInput{
AuthenticationMethod: gotwi.AuthenMethodOAuth2BearerToken,
HTTPClient: &http.Client{
Timeout: time.Duration(120) * time.Second,
},
}
gotwiClient, err := gotwi.NewClient(in)
}If you do not want a timeout, set Timeout: 0. See the http package implementation below.
http package - net/http - Go Packages
-
Set environment variables.
export GOTWI_API_KEY=your-api-key export GOTWI_API_KEY_SECRET=your-api-key-secret
-
Create a search stream rule.
go run . create 'Twitter'
-
Call filtered stream API.
go run . stream -
List search stream rules.
go run . list -
Delete a search stream rule.
go run . delete rule-id