项目作者: Lonami

项目描述 :
dumb telegram bot for python.
高级语言: Python
项目地址: git://github.com/Lonami/dumbot.git
创建时间: 2018-04-21T13:43:14Z
项目社区:https://github.com/Lonami/dumbot

开源协议:MIT License

下载


dumbot

dumb async telegram bot for python 3.

installation

add dumbot.py to your project or pip install dumbot.

usage

basic

  1. import asyncio
  2. from dumbot import Bot
  3. async def main():
  4. bot = Bot(token)
  5. print(await bot.getMe())
  6. msg = await bot.sendMessage(chat_id=10885151, text='hi lonami')
  7. if msg.ok:
  8. print('message sent', msg)
  9. else:
  10. print('something went wrong!', msg)
  11. loop = asyncio.get_event_loop()
  12. loop.run_until_complete(main())

files

  1. async def main():
  2. ...
  3. await bot.sendDocument(chat_id=10885151, file=dict(
  4. type='document',
  5. file='/home/lonami/holiday.jpg'
  6. ))

updates

  1. async def on_update(update):
  2. await bot.sendMessage(
  3. chat_id=update.message.chat.id,
  4. text=update.message.text[::-1]
  5. )
  6. ...
  7. bot.on_update = on_update
  8. bot.run()

subclassing

  1. class Subbot(Bot):
  2. async def init(self):
  3. self.me = await self.getMe()
  4. async def on_update(self, update):
  5. await self.sendMessage(
  6. chat_id=update.message.chat.id,
  7. text='i am {}'.format(self.me.username)
  8. )
  9. Subbot(token).run()

faq

what methods are available?

https://core.telegram.org/bots/api.

can i send opened files or bytes directly?

yes.

can i change a document’s filename or mime?

yes, with name or mime fields in the dict.

how can i handle exceptions?

there aren’t, simply check the .ok property.

what’s the return value?

a magic object, accessing unknown properties returns a false-y magic object:

  1. from dumbot import Obj
  2. lonami = Obj(name='lonami', hobby='developer')
  3. print(lonami.name, 'is', lonami.age or 20)
  4. lonami.friend.name = 'kate'
  5. print(lonami.friend)

no dependencies?

python alone is enough dependencies.

how does this work without urllib or aiohttp?

it’s simple, we construct http requests manually.

why would you reimplement http?

it’s a fun good learning experience, and avoids bloat dependencies.

what do you have against uppercase?

scary. there would be less upper case if it weren’t for
python’s naming conventions or telegram’s for that matter.