semvec/pypi-packages huggingface dataset:
keralagram 0.1.10
Asynchronous Python API for building Telegram bots
License: MIT
Keywords: telegram asyncio bot

# KeralaGram

A python telegram asynchronous bot api made using [aiohttp](https://github.com/aio-libs/aiohttp).
This lib was earlier binded with [httpx](https://pypi.org/project/httpx/). But after the initial release we found it much slower than aiohttp.
So we switched to aiohttp


## Getting started.

* Installation using pip:

```
$ pip install keralagram
```

* While the API is production-ready, it is still under development, and it has regular updates, do not forget to update it regularly by calling `pip install keralagram --upgrade`

## Writing your first bot

### Prerequisites

[Get an API token via @BotFather](https://core.telegram.org/bots#botfather). We will call this token `TOKEN`.
Furthermore, you have basic knowledge of the Python programming language and more importantly [the Telegram Bot API](https://core.telegram.org/bots/api).

### A simple command bot

```python
from keralagram import KeralaGram, Dispatcher
from keralagram.types import Message

bot = KeralaGram("TOKEN")
dp = Dispatcher(bot)

# you can use a list of prefixes or a single one 
# Also if the prefixes value is none defaults to "/"
@dp.on_command("start", prefixes=["!", "/"])
async def start(c: KeralaGram, m: Message):
    await m.reply_text("hello")

if __name__ == '__main__':
    dp.run()
```

## The Telegram Chat Group

Get help. Discuss. Chat.

* Join the [Kerala Telegram Chat Group](https://telegram.me/keralasbot)
* We now have a Telegram Channel as well! Keep yourself up to date with API changes, and [join it](https://telegram.me/keralasbotnews).

## More examples

WILL BE SOON PUBLISHED
expectations 1.0.1
Asynchronous expectations (Jobs/tasks) for future events.
License: MIT
Requires Python: >=3.9,<4.0
Keywords: expectations,expect,python,asyncio,async,asynchronous,jobs,tasks,future

# Expectations for Python

**Asynchronous expectations (Jobs/tasks) for future events.**

If client user sends a message to server, a new eid is generated and sent with the message. If server sends back a message
with an eid, the eid is checked against our expectations. If eid matches an expectation, that expectation's callback
will run. If server sends a message without an eid, no expectations will be checked.

## Installation

Pypi package is present.

`pip install expectations`

## Implementation

**Important steps are marked 1-4.**

```python
import asyncio
import os
import json
import aiohttp
from expectations import Expector, eidgen

HOST = os.getenv('HOST', '127.0.0.1')
PORT = int(os.getenv('PORT', 1339))

URL = f'http://{HOST}:{PORT}/ws'


def my_expected_proc(data):  # Simple function we will use as our expectation callback.
	print('Processed expectation:', data)


async def main():
	session = aiohttp.ClientSession()
	expector = Expector()  # 1: Get setup.
	async with session.ws_connect(URL) as ws:
		async for msg in ws:
			try:
				server_message = msg.data.rstrip()
				j = json.loads(server_message)

				if 'eid' in j:
					# 4: See if incoming eid from server matches any of our expectations.
					expected = await expector.check_expectations(j['eid'], server_message)
					if expected:
						# Give expected callback a second to process (For demo purposes; so it runs before prompt)
						await asyncio.sleep(1000)
					else:
						raise Exception(f'Unexpected message from server: {msg}')
			except Exception as ex:
				print(ex)
			finally:
				await prompt_and_send(ws, expector)

			if msg.type in (aiohttp.WSMsgType.CLOSED,
			                aiohttp.WSMsgType.ERROR):
				break


async def prompt_and_send(ws, expector):
	user_msg = input('Type a message to send to the server: ')
	if user_msg == 'exit':
		print('Exiting!')
		raise SystemExit(0)

	eid = eidgen()  # 2: Create new expectation ID (eid).
	await expector.expect(eid, my_expected_proc)  # 3: Create new expectation using eid and any callback function.

	await ws.send_str(json.dumps({
		'method': user_msg,
		'eid': eid  # eid is sent with msg (For demo). If we get this eid back later, the expectation callback will run.
	}))


if __name__ == '__main__':
	print('Type "exit" to quit')
	loop = asyncio.get_event_loop()
	loop.run_until_complete(main())

```


**MIT License**