Skip to content

Commit 8040716

Browse files
Hello World
1 parent 8a90ffb commit 8040716

File tree

9 files changed

+162
-1
lines changed

9 files changed

+162
-1
lines changed

LICENSE

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Copyright (c) 2024 Anorboyev Shahobiddin
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy
4+
of this software and associated documentation files (the "Software"), to deal
5+
in the Software without restriction, including without limitation the rights
6+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
copies of the Software, and to permit persons to whom the Software is
8+
furnished to do so, subject to the following conditions:
9+
10+
The above copyright notice and this permission notice shall be included in all
11+
copies or substantial portions of the Software.
12+
13+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19+
SOFTWARE.

README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,26 @@
11
# picogram
22
small api library for telegram bot api
3+
4+
5+
Installation:
6+
```shell
7+
pip install picogram
8+
```
9+
10+
11+
Example:
12+
13+
```python
14+
from picogram import Bot
15+
16+
bot = Bot(token='your_token')
17+
18+
19+
@bot.message()
20+
def start_message(message: dict):
21+
bot.send_message(chat_id=message['chat']['id'], text='Hello!')
22+
23+
24+
if __name__ == '__main__':
25+
bot.run_polling()
26+
```

api_request.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
from pprint import pprint
2+
3+
import requests
4+
import time
5+
6+
API_URL = 'https://api.telegram.org/bot'
7+
BOT_TOKEN = 'your_token_here'
8+
TEXT = 'Update!'
9+
MAX_COUNTER = 200
10+
11+
offset = -2
12+
counter = 0
13+
chat_id: int
14+
15+
while counter < MAX_COUNTER:
16+
17+
print('attempt =', counter)
18+
19+
updates = requests.get(f'{API_URL}{BOT_TOKEN}/getUpdates?offset={offset + 1}').json()
20+
pprint(updates)
21+
22+
if updates['result']:
23+
print("Something is here", counter)
24+
for result in updates['result']:
25+
offset = result['update_id']
26+
chat_id = result['message']['from']['id']
27+
requests.get(f'{API_URL}{BOT_TOKEN}/sendMessage?chat_id={chat_id}&text={TEXT}')
28+
29+
time.sleep(0.5)
30+
counter += 1

example.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from picogram import Bot
2+
3+
bot = Bot(token='your_token_here')
4+
5+
6+
@bot.message()
7+
def start_message(message: dict):
8+
bot.send_message(chat_id=message['chat']['id'], text='Hello!')
9+
10+
11+
if __name__ == '__main__':
12+
bot.run_polling()

main.py

Lines changed: 0 additions & 1 deletion
This file was deleted.

picogram/__init__.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/usr/bin/env python
2+
3+
"""A library that provides a Python interface to the Telegram Bots API"""
4+
5+
__author__ = 'Shahobiddin Anorboyev'
6+
__version__ = '0.0.1'
7+
8+
from .main import Bot
9+
10+
11+
__all__ = ['Bot']

picogram/main.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import requests
2+
3+
from pprint import pprint
4+
5+
6+
class Bot:
7+
8+
def __init__(self, token):
9+
self.api_url = 'https://api.telegram.org/bot'
10+
self.token = token
11+
self.handlers = []
12+
13+
def run_polling(self):
14+
offset = -2
15+
update_id = None
16+
while True:
17+
updates = requests.get(f'{self.api_url}{self.token}/getUpdates?offset={offset + 1}').json()
18+
if updates['result']:
19+
last_update_id = updates['result'][-1]['update_id']
20+
if last_update_id != update_id:
21+
pprint(updates)
22+
update_id = last_update_id
23+
for handler in self.handlers:
24+
handler(message=updates['result'][-1]['message'])
25+
26+
def message(self):
27+
def decorator(fn):
28+
self.handlers.append(fn)
29+
return fn
30+
31+
return decorator
32+
33+
def send_message(self, chat_id, text):
34+
requests.get(f'{self.api_url}{self.token}/sendMessage?chat_id={chat_id}&text={text}')

picogram/version.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
__version__ = '0.0.1'

pyproject.toml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
[build-system]
2+
requires = ["hatchling"]
3+
build-backend = "hatchling.build"
4+
5+
[project]
6+
name = "picogram"
7+
version = "0.0.1"
8+
authors = [
9+
{ name="Shahobiddin Anorboyev", email="[email protected]" },
10+
]
11+
maintainers = [
12+
{ name="Shahobiddin Anorboyev", email="[email protected]" },
13+
]
14+
keywords = ["telegram", "bot", "api", "library", "small"]
15+
dependencies = [
16+
"requests",
17+
]
18+
description = "small api library for telegram bot api"
19+
readme = "README.md"
20+
requires-python = ">=3.8"
21+
classifiers = [
22+
"Programming Language :: Python :: 3",
23+
"License :: OSI Approved :: MIT License",
24+
"Operating System :: OS Independent",
25+
]
26+
27+
[project.urls]
28+
Homepage = "https://github.com/anorprogrammer/picogram"
29+
Issues = "https://github.com/anorprogrammer/picogram/issues"
30+
31+

0 commit comments

Comments
 (0)