telega 0.2.1
Telegram Bot API implementation
To use this package, run the following command in your project's root directory:
Manual usage
Put the following dependency into your project's dependences section:
Telega
Telegram bot API implementation.
Run examples
To run examples from examples/
dir you need to create a bot 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
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
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:
// 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 and PR's are welcome.
- Registered by Pavel Chebotarev
- 0.2.1 released 11 months ago
- nexor/telega
- MIT
- Authors:
- Dependencies:
- dests, vibe-core, asdf
- Versions:
-
0.2.1 2024-Jan-07 0.2.0 2020-Dec-05 0.1.2 2020-Jul-01 0.1.1 2020-Jun-21 0.1.0 2020-Jun-20 - Download Stats:
-
-
0 downloads today
-
0 downloads this week
-
3 downloads this month
-
317 downloads total
-
- Score:
- 0.9
- Short URL:
- telega.dub.pm