Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/hangyas/telegram_bot
(deprecated) see https://github.com/protoncr/tourmaline instead
https://github.com/hangyas/telegram_bot
bot crystal telegram telegram-bot
Last synced: 3 months ago
JSON representation
(deprecated) see https://github.com/protoncr/tourmaline instead
- Host: GitHub
- URL: https://github.com/hangyas/telegram_bot
- Owner: hangyas
- License: mit
- Created: 2015-10-30T14:56:25.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2020-04-14T17:06:11.000Z (over 4 years ago)
- Last Synced: 2024-05-23T03:14:47.446Z (6 months ago)
- Topics: bot, crystal, telegram, telegram-bot
- Language: Crystal
- Homepage:
- Size: 128 KB
- Stars: 71
- Watchers: 9
- Forks: 16
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-crystal - telegram_bot - A wrapper for the Telegram Bot API (Third-party APIs)
README
❌❌❌❌❌❌❌❌
# Deprecation Notice
Please consider using [tourmaline](https://github.com/protoncr/tourmaline) instead
I'll try to keep this library compatible with the future versions of Crystal, but tourmaline is a well designed and maintained library which is superior in every way while sharing the same basics, thus I don't see any value in implementing the same improvements and fragmenting the community
Bug fixes are still welcomed
❌❌❌❌❌❌❌❌
# TelegramBot
[Telegram Bot API](https://core.telegram.org/bots/api) (3.2) wrapper for Crystal
## Current features
api methods and types:
- [x] basic message types
- [x] stickers
- [x] inline mode
- [x] payments
- [x] gamesgetting updates:
- [x] long polling
- [x] webhooksadditional features:
- [x] white & black lists
- [x] command handler## Usage
Create your bot by inheriting from `TelegramBot::Bot`.
### Commands
Define which commands your bot handles via the `cmd` method in the `CmdHandler` module. For example, respond `world` to `/hello` and perform simple calculation with `/add`:
```crystal
require "telegram_bot"class MyBot < TelegramBot::Bot
include TelegramBot::CmdHandlerdef initialize
super("MyBot", TOKEN)cmd "hello" do |msg|
reply msg, "world!"
end# /add 5 7 => 12
cmd "add" do |msg, params|
reply msg, "#{params[0].to_i + params[1].to_i}"
end
end
endmy_bot = MyBot.new
my_bot.polling
```### Logging
```crystal
MyBot::Log.level = :debug
MyBot::Log.backend = ::Log::IOBackend.newmy_bot = MyBot.new
my_bot.polling
```### Custom handlers
Override any of the following `handle` methods to handle Telegram updates, be it [messages](https://core.telegram.org/bots/api#message), [inline queries](https://core.telegram.org/bots/api#inlinequery), [chosen inline results](https://core.telegram.org/bots/api#choseninlineresult) or [callback queries](https://core.telegram.org/bots/api#callbackquery):
```crystal
def handle(message : Message)def handle(inline_query : InlineQuery)
def handle(chosen_inline_result : ChosenInlineResult)
def handle(callback_query : CallbackQuery)
def handle_edited(message : Message)
def handle_channel_post(message : Message)
def handle_edited_channel_post(message : Message)
```For example, to echo all messages sent to the bot:
```crystal
class EchoBot < TelegramBot::Bot
def handle(message : Message)
if text = message.text
reply message, text
end
end
endEchoBot.new.polling
```Or to answer inline queries with a list of articles:
```crystal
class InlineBot < TelegramBot::Bot
def handle(inline_query : TelegramBot::InlineQuery)
results = Array(TelegramBot::InlineQueryResult).newcontent = InputTextMessageContent.new "Article details"
results << TelegramBot::InlineQueryResultArticle.new("article/1", "My first article", content)answer_inline_query(inline_query.id, results)
end
endInlineBot.new.polling
```Remember to [enable inline mode](https://core.telegram.org/bots/api#inline-mode) in BotFather to support inline queries.
### Webhooks
All the examples above use the [`getUpdates`](https://core.telegram.org/bots/api#getupdates) method, constantly polling Telegram for new updates, by invoking the `polling` method on the bot.
Another option is to use the [`setWebhook`](https://core.telegram.org/bots/api#setwebhook) method to tell Telegram where to POST any updates for your bot. Note that you __must__ use HTTPS in this endpoint for Telegram to work, and you can use a self-signed certificate, which you can provide as part of the `setWebhook` method:
```crystal
# Certificate has the contents of the certificate, not the path to it
bot.set_webhook(url, certificate)
```After invoking `setWebhook`, have your bot start an HTTPS server with the `serve` command:
```crystal
bot.serve("0.0.0.0", 443, "path/to/ssl/certificate", "path/to/ssl/key")
```If you run your bot behind a proxy that performs SSL offloading (ie the proxy presents the certificate to Telegram, and then forwards the request to your app using plain HTTP), you may skip the last two parameters, and the bot will listen for HTTP requests instead of HTTPS.
When running your bot in `serve` mode, the bot will favour executing any methods by sending a response as part of the Telegram request, rather than executing a new request.
### White/blacklists
However it's not part of the API you can set black or white lists in the bot's constructor to filter your users by username.
`whitelist`: if user is not present on the list (or doesn't have username) the message won't be handled
`blacklist`: if user is present on the list the message won't be handled
## Installation
Add this to your application's `shard.yml`:
```yaml
dependencies:
telegram_bot:
github: hangyas/telegram_bot
```## Contributing
__Contributing is very welcomed!__
1. Fork it ( https://github.com/hangyas/TelegramBot/fork )
2. Create your feature branch (git checkout -b my-new-feature)
3. Commit your changes (git commit -am 'Add some feature')
4. Push to the branch (git push origin my-new-feature)
5. Create a new Pull Request## Contributors
- [hangyas](https://github.com/hangyas) Krisztián Ádám - creator, maintainer