❌❌❌❌❌❌❌❌
DeprecationNoticePleaseconsiderusingtourmalineinstead
I'lltrytokeepthislibrarycompatiblewiththefutureversionsofCrystal,buttourmalineisawelldesignedandmaintainedlibrarywhichissuperiorineverywaywhilesharingthesamebasics,thusIdon'tseeanyvalueinimplementingthesameimprovementsandfragmentingthecommunity
Bugfixesarestillwelcomed
❌❌❌❌❌❌❌❌
TelegramBotTelegramBotAPI(3.2)wrapperforCrystal
Currentfeaturesapimethodsandtypes:
basicmessagetypesstickersinlinemodepaymentsgamesgettingupdates:
longpollingwebhooksadditionalfeatures:
white&blacklistscommandhandlerUsageCreateyourbotbyinheritingfromTelegramBot::Bot.
CommandsDefinewhichcommandsyourbothandlesviathecmdmethodintheCmdHandlermodule.Forexample,respondworldto/helloandperformsimplecalculationwith/add:
require"telegram_bot"classMyBot<TelegramBot::BotincludeTelegramBot::CmdHandlerdefinitializesuper("MyBot",TOKEN)cmd"hello"do|msg|replymsg,"world!"end#/add57=>12cmd"add"do|msg,params|replymsg,"#{params[0].to_i+params[1].to_i}"endendendmy_bot=MyBot.newmy_bot.pollingLoggingMyBot::Log.level=:debugMyBot::Log.backend=::Log::IOBackend.newmy_bot=MyBot.newmy_bot.pollingCustomhandlersOverrideanyofthefollowinghandlemethodstohandleTelegramupdates,beitmessages,inlinequeries,choseninlineresultsorcallbackqueries:
defhandle(message:Message)defhandle(inline_query:InlineQuery)defhandle(chosen_inline_result:ChosenInlineResult)defhandle(callback_query:CallbackQuery)defhandle_edited(message:Message)defhandle_channel_post(message:Message)defhandle_edited_channel_post(message:Message)Forexample,toechoallmessagessenttothebot:
classEchoBot<TelegramBot::Botdefhandle(message:Message)iftext=message.textreplymessage,textendendendEchoBot.new.pollingOrtoanswerinlinequerieswithalistofarticles:
classInlineBot<TelegramBot::Botdefhandle(inline_query:TelegramBot::InlineQuery)results=Array(TelegramBot::InlineQueryResult).newcontent=InputTextMessageContent.new"Articledetails"results<<TelegramBot::InlineQueryResultArticle.new("article/1","Myfirstarticle",content)answer_inline_query(inline_query.id,results)endendInlineBot.new.pollingRemembertoenableinlinemodeinBotFathertosupportinlinequeries.
WebhooksAlltheexamplesaboveusethegetUpdatesmethod,constantlypollingTelegramfornewupdates,byinvokingthepollingmethodonthebot.
AnotheroptionistousethesetWebhookmethodtotellTelegramwheretoPOSTanyupdatesforyourbot.NotethatyoumustuseHTTPSinthisendpointforTelegramtowork,andyoucanuseaself-signedcertificate,whichyoucanprovideaspartofthesetWebhookmethod:
#Certificatehasthecontentsofthecertificate,notthepathtoitbot.set_webhook(url,certificate)AfterinvokingsetWebhook,haveyourbotstartanHTTPSserverwiththeservecommand:
bot.serve("0.0.0.0",443,"path/to/ssl/certificate","path/to/ssl/key")IfyourunyourbotbehindaproxythatperformsSSLoffloading(ietheproxypresentsthecertificatetoTelegram,andthenforwardstherequesttoyourappusingplainHTTP),youmayskipthelasttwoparameters,andthebotwilllistenforHTTPrequestsinsteadofHTTPS.
Whenrunningyourbotinservemode,thebotwillfavourexecutinganymethodsbysendingaresponseaspartoftheTelegramrequest,ratherthanexecutinganewrequest.
White/blacklistsHoweverit'snotpartoftheAPIyoucansetblackorwhitelistsinthebot'sconstructortofilteryourusersbyusername.
whitelist:ifuserisnotpresentonthelist(ordoesn'thaveusername)themessagewon'tbehandled
blacklist:ifuserispresentonthelistthemessagewon'tbehandled
InstallationAddthistoyourapplication'sshard.yml:
dependencies:telegram_bot:github:hangyas/telegram_botContributingContributingisverywelcomed!
Forkit(https://github.com/hangyas/TelegramBot/fork)Createyourfeaturebranch(gitcheckout-bmy-new-feature)Commityourchanges(gitcommit-am'Addsomefeature')Pushtothebranch(gitpushoriginmy-new-feature)CreateanewPullRequestContributorshangyasKrisztiánÁdám-creator,maintainer
评论