telegram_bot开源项目

我要开发同款
匿名用户2021年11月08日
43阅读
开发技术Python
所属分类应用工具、IM/聊天/语音工具
授权协议MIT License

作品详情

❌❌❌❌❌❌❌❌

DeprecationNotice

Pleaseconsiderusingtourmalineinstead

I'lltrytokeepthislibrarycompatiblewiththefutureversionsofCrystal,buttourmalineisawelldesignedandmaintainedlibrarywhichissuperiorineverywaywhilesharingthesamebasics,thusIdon'tseeanyvalueinimplementingthesameimprovementsandfragmentingthecommunity

Bugfixesarestillwelcomed

❌❌❌❌❌❌❌❌

TelegramBot

TelegramBotAPI(3.2)wrapperforCrystal

Currentfeatures

apimethodsandtypes:

basicmessagetypesstickersinlinemodepaymentsgames

gettingupdates:

longpollingwebhooks

additionalfeatures:

white&blacklistscommandhandlerUsage

CreateyourbotbyinheritingfromTelegramBot::Bot.

Commands

DefinewhichcommandsyourbothandlesviathecmdmethodintheCmdHandlermodule.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.pollingCustomhandlers

OverrideanyofthefollowinghandlemethodstohandleTelegramupdates,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.polling

Ortoanswerinlinequerieswithalistofarticles:

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.polling

RemembertoenableinlinemodeinBotFathertosupportinlinequeries.

Webhooks

AlltheexamplesaboveusethegetUpdatesmethod,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/blacklists

Howeverit'snotpartoftheAPIyoucansetblackorwhitelistsinthebot'sconstructortofilteryourusersbyusername.

whitelist:ifuserisnotpresentonthelist(ordoesn'thaveusername)themessagewon'tbehandled

blacklist:ifuserispresentonthelistthemessagewon'tbehandled

Installation

Addthistoyourapplication'sshard.yml:

dependencies:telegram_bot:github:hangyas/telegram_botContributing

Contributingisverywelcomed!

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
声明:本文仅代表作者观点,不代表本站立场。如果侵犯到您的合法权益,请联系我们删除侵权资源!如果遇到资源链接失效,请您通过评论或工单的方式通知管理员。未经允许,不得转载,本站所有资源文章禁止商业使用运营!
下载安装【程序员客栈】APP
实时对接需求、及时收发消息、丰富的开放项目需求、随时随地查看项目状态

评论