带Tornado的Telegram Bot API

wcpan.telegram的Python项目详细描述


电报机器人API与龙卷风。

高级API示例

TeleLich是高级不死生物,您可以要求它发送请求:

fromtornadoimportgen,ioloopfromtelezombieimportapi,types@gen.coroutinedefmain():API_TOKEN='your_token'lich=api.TeleLich(API_TOKEN)talk_to=42# getMeuser=yieldlich.get_me()print(user)# getUpdatesupdates=yieldlich.get_updates()print(updates)# sendMessagemessage=yieldlich.send_message(talk_to,'hello')print(message)# sendPhotophoto=types.InputFile('path_to_your_phoho.png')message=yieldlich.send_photo(talk_to,photo)print(message)# sendAudioaudio=types.InputFile('path_to_your_audio.ogg')message=yieldlich.send_audio(talk_to,audio)print(message)# sendVideovideo=types.InputFile('path_to_your_video.mp4')message=yieldlich.send_video(talk_to,video)print(message)# sendDocumentdocument=types.InputFile('path_to_your_file.zip')message=yieldlich.send_document(talk_to,document)print(message)main_loop=ioloop.IOLoop.instance()main_loop.run_sync(main)

让它处理更新:

fromtornadoimportgen,ioloopfromtelezombieimportapiclassKelThuzad(api.TeleLich):def__init__(self,api_token):super(KelThuzad,self).__init__(api_token)@gen.coroutinedefon_text(self,message):id_=message.message_idchat=message.chattext=message.text# echo same textyieldself.send_message(chat.id_,text,reply_to_message_id=id_)@gen.coroutinedefon_video(self,message):chat=message.chatvideo=message.video# echo video without upload againyieldself.send_video(chat.id_,video.file_id,reply_to_message_id=id_)@gen.coroutinedefforever():API_TOKEN='your_token'lich=api.KelThuzad(API_TOKEN)yieldlich.poll()main_loop=ioloop.IOLoop.instance()main_loop.run_sync(forever)

或通过webhook处理更新:

fromtornadoimportgen,ioloop,webfromtelezombieimportapiclassHookHandler(api.TeleHookHandler):@gen.coroutinedefon_text(self,message):lich=self.application.settings['lich']id_=message.message_idchat=message.chattext=message.text# echo same textyieldlich.send_message(chat.id_,text,reply_to_message_id=id_)@gen.coroutinedefcreate_lich():API_TOKEN='your_token'lich=api.TeleLich(API_TOKEN)yieldlich.listen('https://your.host/hook')returnlichmain_loop=ioloop.IOLoop.instance()lich=main_loop.run_sync(create_lich)application=web.Application([(r"/hook",HookHandler),],lich=lich)# please configure your own SSL proxyapplication.listen(8000)main_loop.start()

低级API示例

虽然TeleLich很方便,但有时您只需要一个食尸鬼 为你收集木材,这样你就可以用它来建造你自己的自贡。

TeleZombie提供简单直接的api映射:

fromtornadoimportgen,ioloopfromtelezombieimportapi,types@gen.coroutinedefmain():API_TOKEN='your_token'ghoul=api.TeleZombie(API_TOKEN)talk_to=42# getMeuser=yieldghoul.get_me()print(user)# getUpdatesoffset=0updates=[]whileTrue:us=yieldghoul.get_updates(offset)updates.extend(us)ifnotus:breakoffset=us[-1].update_id+1print(updates)# sendMessagemessage=yieldghoul.send_message(talk_to,'hello')print(message)# sendDocumentdocument=types.InputFile('path_to_your_file.zip')message=yieldlich.send_document(talk_to,document)print(message)main_loop=ioloop.IOLoop.instance()main_loop.run_sync(main)

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java JDBC(MS SQL)禁用所有传输以执行sp_replicationdboption语句   java运行时外部数据加载   运行gradlew build时出现java ListenerNotificationException   java如何使用按钮列表进行onClick(GWT)   java对滚动视图上具有特定资源ID的所有元素进行计数   要在数据库中插入的spring Java注释   Gradle Kotlin应用程序的java初学者脚本找不到主类   java在ms access中保存jpeg格式,并在swing的面板上显示   java存储字符串:最终类与外部存储中的文件   Java注释ElementType常量是什么意思?   java是ByteArrayInputStream真的是流吗?   java Eclipse项目和Maven pom文件不在同一目录中