Skip to content
Kotlin DSL client for TamTam Bot API
Branch: master
Clone or download
Namazed Migrate to api 0.1.5. Merge #pr11.
Added id property to media attachments (VideoAttachment, AudioAttachment) so you can reuse attachment from one message in another
Added ability to create linked message: replied or forwarded. See link in NewMessageBody
Intent property marked as required only for CallbackButton
Up kotlin version and serialization version
Created simple dsl requests for send LinkMessage
Latest commit ee0e065 Apr 22, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
gradle/wrapper Added sample class with main dsl methods. Splited project on two modu… Mar 13, 2019
library Up version of kotlin plugin. Created dsl methods for answer with noti… Apr 19, 2019
sample Fixed serialization for answerCallback, sendMessage and attachments. … Apr 19, 2019
.gitignore Added dsl marker for scopes, created class for parsing of updates. Cr… Jan 23, 2019
LICENSE Add LICENSE Jan 23, 2019
README.md Up library version to 0.2.2 Apr 18, 2019
build.gradle.kts Up version of kotlin plugin. Created dsl methods for answer with noti… Apr 19, 2019
gradlew Init project and gradle. Created http client for bot api with some re… Jan 23, 2019
gradlew.bat Init project and gradle. Created http client for bot api with some re… Jan 23, 2019
settings.gradle

README.md

TamTamBotApiClientDsl

Kotlin DSL for TamTam Bot API. Using this library you can simplify working with the Bot API. What this library helps with:

  • Subscribes to updates using LongPolling (WebHook will be later)
  • Coordinates updates to specific areas of responsibility that you describe. All updates are processed in parallel.
  • Handles commands
  • Provides a convenient interface for working with requests to the Bot API

The bot API documentation is here.

What you need to start

You should build your project with JDK 8.

To access the library, you must add the dependency on jitpack and kotlinx

Gradle

allprojects {
	repositories {
		...
		maven { url 'https://dl.bintray.com/kotlin/kotlinx' }
		maven { url 'https://jitpack.io' }
	}
}

Maven

<repositories>
	<repository>
	    <id>kotlinx</id>
	    <url>https://dl.bintray.com/kotlin/kotlinx</url>
	</repository>
	<repository>
	    <id>jitpack.io</id>
	    <url>https://jitpack.io</url>
	</repository>
</repositories>

You must also add a dependency.

Gradle

dependencies {
    implementation 'com.github.Namazed:TamTamBotApiClientDsl:0.2.2'
}

Maven

<dependency>
	 <groupId>com.github.Namazed</groupId>
	 <artifactId>TamTamBotApiClientDsl</artifactId>
	 <version>0.2.2</version>
</dependency>

Examples

The example of a finished bot can be found here.

Scopes

Below you find example of how start process sync longPolling on main thread:

fun main() {
  longPolling("BOT_TOKEN") {

  }
}

Async longPolling on other single thread:

fun main() {
  longPolling("BOT_TOKEN", true) {

  }
}

All other methods are called from longPolling scope (lambda). In order to subscribe to the launch of the bot, just call the method:

onStartBot { startedBotState ->
    //some actions
}

If you want subscribe on specific command (for example, /actions):

commands {

    onCommand("/actions") { commandState ->
        //some actions
    }

    onUnknownCommand { commandState ->
        //some actions
    }

}

To work with Callbacks (i.e. when the user pushed a button on the keyboard that your bot sent him), need to create scope callbacks:

callbacks {

    defaultAnswer { callbackState ->
        //some actions
    }

    answerOnCallback("test_button_one") { callbackState ->
        //some actions
    }

    answerOnCallback("test_button_two") { callbackState ->
        //some actions
    }
}

The update about click on button will fall into this lambda.

And last from main scopes is the scope of processing new messages from the user (which do not contain the command):

messages {

    answerOnMessage { messageState ->
        //some actions
    }

}

Also have this methods:

  • onAddBotToChat
  • onRemoveBotFromChat
  • Users scope, in which there are necessary methods for processing updates associated with adding a user to the chat or deleting it

In all the lambdas that were mentioned above, there is a State parameter that contains all the necessary information about the current update. For example, a CommandState contains a timestamp when an event occurred (that is, the user sent you a command) and the Command class.

Requests

In most cases, requests can be sent using a convenient DSL, or by directly calling methods on the class RequestsManager, which is available in all Scopes. For example, if you need to send some text to the user, simply call the sendFor or sendText:

With DSL

"Two bananas" sendFor commandState.command.message.sender.userId

With RequestsManager

requestsManager.sendText(commandState.command.message.sender.userId, "Two bananas")

To create an InlineKeyboard, you can use the keyboard DSL. Below is an example of creating InlineKeyboard in three ways using: operator unaryPlus, via infix function add, or simply call the function add. The buttonRow creates a row with buttons.

keyboard {
    +buttonRow {
        +Button(
            ButtonType.CALLBACK,
            "Create dreams",
            payload = "DREAMS"
        )
        +Button(
            ButtonType.CALLBACK,
            "Imagine that you are Dragon",
            payload = "DRAGON"
        )
    }

    this add buttonRow {
        this add Button(
            ButtonType.LINK,
            "Find new dreams",
	    url = "dreams.com"
        )
    }

    add(buttonRow {
        add(
            Button(
                ButtonType.LINK,
                "Find new dreams",
		url = "dreams.com"
            )
        )
    })
}

Dependencies

Contributing

Pull requests are welcome. If you find any bugs or have a great idea, please create an issue for this.

License

This library uses a license Apache 2.0.

You can’t perform that action at this time.