https://github.com/nexor/telega
Telegram Bot API implementation
https://github.com/nexor/telega
dlang telegram telegram-bot-api
Last synced: 4 months ago
JSON representation
Telegram Bot API implementation
- Host: GitHub
- URL: https://github.com/nexor/telega
- Owner: nexor
- License: mit
- Created: 2018-04-07T13:13:32.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2026-02-13T13:17:33.000Z (4 months ago)
- Last Synced: 2026-02-13T22:29:25.255Z (4 months ago)
- Topics: dlang, telegram, telegram-bot-api
- Language: D
- Size: 193 KB
- Stars: 26
- Watchers: 3
- Forks: 5
- Open Issues: 13
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# Telega
[Telegram bot API](https://core.telegram.org/bots/api) implementation.
[](http://code.dlang.org/packages/telega)
[](https://travis-ci.org/nexor/telega)
## Run examples
To run examples from `examples/` dir you need to [create a bot](https://core.telegram.org/bots#6-botfather) and
put it as BOT_TOKEN variable in `.env` file at the root of this repository.
```
$ cat .env
BOT_TOKEN=123456789:BotTokenHere
```
Now you can run examples using make command
```
$ make run-example-echobot
$ make run-example-keyboard
$ make run-example-pollbot
```
## Quickstart
Simple echo bot example
```d
import vibe.core.core : runApplication, runTask;
import vibe.core.log : setLogLevel, logInfo, LogLevel;
import std.process : environment;
import std.exception : enforce;
int main(string[] args)
{
string botToken = environment.get("BOT_TOKEN");
if (args.length > 1 && args[1] != null) {
logInfo("Setting token from first argument");
botToken = args[1];
}
enforce(botToken !is null, "Please provide bot token as a first argument or set BOT_TOKEN env variable");
setLogLevel(LogLevel.diagnostic);
runTask(&listenUpdates, botToken);
return runApplication();
}
void listenUpdates(string token)
{
import telega.botapi : BotApi;
import telega.telegram.basic : Update, getUpdates, sendMessage;
import std.algorithm.iteration : filter, each;
import std.algorithm.comparison : max;
int offset;
auto api = new BotApi(token);
while (true)
{
api.getUpdates(offset)
.filter!(u => !u.message.text.isNull) // we need all updates with text message
.each!((Update u) {
logInfo("Text from %s: %s", u.message.chat.id, u.message.text);
api.sendMessage(u.message.chat.id, u.message.text);
offset = max(offset, u.id)+1;
});
}
}
```
## Installation
You can add package to your project using dub:
```
dub add telega
```
## Todo
- [ ] Sending files
- [ ] Inline mode
- [ ] Bot API 4.3 and newer
## Help
You can find some examples in the [examples dir](examples/)
API types and methods can be found in telega.telegram.* modules.
Each method is typically implemented using 3 constructs:
- structure for the method fully describing its signature;
- a function that accepts a few arguments representing required method arguments
- a function that accepts a reference to a method struct for calling method with required and optional arguments
For example:
```d
// full method structure
struct SendMessageMethod
{
mixin TelegramMethod!"/sendMessage";
ChatId chat_id;
string text;
Nullable!ParseMode parse_mode;
Nullable!bool disable_web_page_preview;
Nullable!bool disable_notification;
Nullable!uint reply_to_message_id;
ReplyMarkup reply_markup;
}
// short form - only required args
Message sendMessage(T)(BotApi api, T chatId, string text) if (isTelegramId!T)
// full form - need to build SendTelegramMethod struct first
Message sendMessage(BotApi api, ref SendMessageMethod m)
```
### Some hints
`ChatId` type is actually `long` or `string`
`isTelegramId!T` template checks `T` to be some string or number
### Support
[Issues](https://github.com/nexor/telega/issues) and PR's are welcome.