Code Monkey home page Code Monkey logo

dumbot's Introduction

dumbot

dumb async telegram bot for python 3.

installation

install dependencies:

pip install aiohttp

then simply add dumbot.py to your project.

usage

basic

import asyncio
from dumbot import Bot

async def main():
    bot = Bot(token)
    print(await bot.getMe())
    msg = await bot.sendMessage(chat_id=10885151, text='hi lonami')
    if msg.ok:
        print('message sent', msg)
    else:
        print('something went wrong!', msg)

loop = asyncio.get_event_loop()
loop.run_until_complete(main())

files

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

updates

async def on_update(update):
    await bot.sendMessage(
        chat_id=update.message.chat.id,
        text=update.message.text[::-1]
    )

...
bot.on_update = on_update
bot.run()

subclassing

class Subbot(Bot):
    async def init(self):
        self.me = await self.getMe()

    async def on_update(self, update):
        await self.sendMessage(
            chat_id=update.message.chat.id,
            text='i am {}'.format(self.me.username)
        )

Subbot(token).run()

extra

if you're concerned about speed you can pip install cchardet aiodns as suggested in https://docs.aiohttp.org/en/stable/index.html.

you may also want to pip install ujson to faster serialize outgoing requests.

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:

from dumbot import Obj

lonami = Obj(name='lonami', hobby='developer')
print(lonami.name, 'is', lonami.age or 20)

lonami.friend.name = 'kate'
print(lonami.friend)

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.

dumbot's People

Contributors

lonami avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.