Coder Social home page Coder Social logo

ttapi's Introduction

Turntable API


Join us on Discord https://discord.gg/4AA2DqWpVc


A simple go wrapper for the turntable API.
You'll need to find your AUTH, USERID and ROOMID information with this bookmarklet.

Installation

go get github.com/alaingilbert/ttapi

Examples

Chat bot

This bot responds to anybody who writes "/hello" in the chat.

package main

import (
    "os"
    "github.com/alaingilbert/ttapi"
)

func main() {
    auth := os.Getenv("TTAPI_AUTH")
    userID := os.Getenv("TTAPI_USER_ID")
    roomID := os.Getenv("TTAPI_ROOM_ID")
    bot := ttapi.NewBot(auth, userID, roomID)
    bot.OnSpeak(func(evt ttapi.SpeakEvt) {
        if evt.Text == "/hello" {
            _ = bot.Speakf("Hey! How are you @%s ?", evt.Name)
        }
    })
    bot.Start()
}

More examples here -> https://github.com/alaingilbert/ttapi/tree/master/examples

Debugging

Add the following line in your main function

logrus.SetLevel(logrus.DebugLevel)

That will print on the terminal all the data that you get and all the data that you send.

ttapi's People

Contributors

alaingilbert avatar noiz avatar nugget avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

ttapi's Issues

On startup tmpsong info is wrong

I am looking at trying to check the endsong event and I noticed that on startup the first endsong event we get has different data than after we have already been running. I narrowed the issue down to this line: https://github.com/alaingilbert/ttapi/blob/master/ttapi.go#L286 In my initial testing I changed that line to be b.setTmpSong(roomInfoHash["room"].(map[string]interface{})) to match what is sent for normal song changes here: https://github.com/alaingilbert/ttapi/blob/master/ttapi.go#L219 With that hacky fix I can always get the same json map that I can parse the same way both time. I know there should be some error checking in the fix and not sure how you want to handle that, but if we could get the startup info to match the song change info that would fix the issue I am having parsing the endsong event correctly every time.

Bot goes offline and online constantly when you try and PM it

Hey, think I debugged this issue down to the root cause. Right now it looks like ttapi is only sending a presence update when it receives a heartbeat message from the server or when there is no session. From inspecting the web traffic in Chrome we should actually be sending the presence update every 10 seconds (or whatever interval the server is returning). I see right now by inspecting the websocket from Chrome that I am normally sending a presence update every 10 seconds as seen below:

image

I see in the logrus output when I turn on debug logs that every time ttapi sends out a presence update that a message is returned with an interval that ttapi is then not using:

DEBU[0135] < {"api":"presence.update","client":"web","clientid":"dunno_if_this_is_secret","msgid":20,"status":"available","userauth":"edited_this_out","userid":"edited_this_out_too"} 
DEBU[0135] > ~m~72~m~{"msgid": 20, "now": 1623878255.079107, "success": true, "interval": 10} 

Can we update ttapi to send the presence update at the correct interval? I believe that should fix the issue. For now I just started a goroutine that is sending a presence update manually every 10 seconds and that fixed the issue but I feel like ttapi should properly fix this as well for anyone else that wants to use this library.

Latest code has issues with long websocket messages

With the latest code I am seeing an issue where I hit the error print failed to find message length a lot. It looks like the commit 5b76c84 is the culprit for why this is happening. I see the error on what looks like data from the room.info API call which is very large. If I revert the above commit back to the old handling for the websocket I no longer see the issue. I can reproduce the issue pretty easily by PM'ing the bot a super large message (i.e. ~20 lorem ipsums) and see that the websocket seems to split up the large message over multiple websocket messages. Since you removed the check for if we did not receive the full message length we eventually get a websocket message without the length header at the beginning.

Error when processing OnNoSong/NoSongEvt

Hi there,

I don't know why, but while I can process other events without problems, I get this error when I try to consume the OnNoSong:

bot.OnNoSong(func(e ttapi.NoSongEvt) { /* do something */ })

Here's the error:

ERRO[0453] unexpected crashinterface conversion: interface {} is func(ttapi.NoSongEvt), not func(ttapi.H)
goroutine 100 [running]:
runtime/debug.Stack()
	/.asdf/installs/golang/1.18rc1/go/src/runtime/debug/stack.go:24 +0x65
runtime/debug.PrintStack()
	/.asdf/installs/golang/1.18rc1/go/src/runtime/debug/stack.go:16 +0x19
github.com/alaingilbert/ttapi.SGo.func1.1()
	/Code/go-code/pkg/mod/github.com/alaingilbert/[email protected]/utils.go:24 +0x76
panic({0x123f0a0, 0xc000114960})
	/.asdf/installs/golang/1.18rc1/go/src/runtime/panic.go:838 +0x207
github.com/alaingilbert/ttapi.(*Bot).emit.func1.17()
	/Code/go-code/pkg/mod/github.com/alaingilbert/[email protected]/ttapi.go:514 +0x4d
github.com/alaingilbert/ttapi.SGo.func1()
	/Code/go-code/pkg/mod/github.com/alaingilbert/[email protected]/utils.go:27 +0x3f
created by github.com/alaingilbert/ttapi.SGo
	/Code/go-code/pkg/mod/github.com/alaingilbert/[email protected]/utils.go:20 +0x56

I'm using go1.18rc1 darwin/amd64.

On a side note, is there a reason for not having a struct like EndSongEvt for OnEndSong? Why it uses the map H?

thanks in advance! :-)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.