-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8a90ffb
commit 8040716
Showing
9 changed files
with
162 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
Copyright (c) 2024 Anorboyev Shahobiddin | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,26 @@ | ||
# picogram | ||
small api library for telegram bot api | ||
|
||
|
||
Installation: | ||
```shell | ||
pip install picogram | ||
``` | ||
|
||
|
||
Example: | ||
|
||
```python | ||
from picogram import Bot | ||
|
||
bot = Bot(token='your_token') | ||
|
||
|
||
@bot.message() | ||
def start_message(message: dict): | ||
bot.send_message(chat_id=message['chat']['id'], text='Hello!') | ||
|
||
|
||
if __name__ == '__main__': | ||
bot.run_polling() | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
from pprint import pprint | ||
|
||
import requests | ||
import time | ||
|
||
API_URL = 'https://api.telegram.org/bot' | ||
BOT_TOKEN = 'your_token_here' | ||
TEXT = 'Update!' | ||
MAX_COUNTER = 200 | ||
|
||
offset = -2 | ||
counter = 0 | ||
chat_id: int | ||
|
||
while counter < MAX_COUNTER: | ||
|
||
print('attempt =', counter) | ||
|
||
updates = requests.get(f'{API_URL}{BOT_TOKEN}/getUpdates?offset={offset + 1}').json() | ||
pprint(updates) | ||
|
||
if updates['result']: | ||
print("Something is here", counter) | ||
for result in updates['result']: | ||
offset = result['update_id'] | ||
chat_id = result['message']['from']['id'] | ||
requests.get(f'{API_URL}{BOT_TOKEN}/sendMessage?chat_id={chat_id}&text={TEXT}') | ||
|
||
time.sleep(0.5) | ||
counter += 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
from picogram import Bot | ||
|
||
bot = Bot(token='your_token_here') | ||
|
||
|
||
@bot.message() | ||
def start_message(message: dict): | ||
bot.send_message(chat_id=message['chat']['id'], text='Hello!') | ||
|
||
|
||
if __name__ == '__main__': | ||
bot.run_polling() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
#!/usr/bin/env python | ||
|
||
"""A library that provides a Python interface to the Telegram Bots API""" | ||
|
||
__author__ = 'Shahobiddin Anorboyev' | ||
__version__ = '0.0.1' | ||
|
||
from .main import Bot | ||
|
||
|
||
__all__ = ['Bot'] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import requests | ||
|
||
from pprint import pprint | ||
|
||
|
||
class Bot: | ||
|
||
def __init__(self, token): | ||
self.api_url = 'https://api.telegram.org/bot' | ||
self.token = token | ||
self.handlers = [] | ||
|
||
def run_polling(self): | ||
offset = -2 | ||
update_id = None | ||
while True: | ||
updates = requests.get(f'{self.api_url}{self.token}/getUpdates?offset={offset + 1}').json() | ||
if updates['result']: | ||
last_update_id = updates['result'][-1]['update_id'] | ||
if last_update_id != update_id: | ||
pprint(updates) | ||
update_id = last_update_id | ||
for handler in self.handlers: | ||
handler(message=updates['result'][-1]['message']) | ||
|
||
def message(self): | ||
def decorator(fn): | ||
self.handlers.append(fn) | ||
return fn | ||
|
||
return decorator | ||
|
||
def send_message(self, chat_id, text): | ||
requests.get(f'{self.api_url}{self.token}/sendMessage?chat_id={chat_id}&text={text}') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
__version__ = '0.0.1' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
[build-system] | ||
requires = ["hatchling"] | ||
build-backend = "hatchling.build" | ||
|
||
[project] | ||
name = "picogram" | ||
version = "0.0.1" | ||
authors = [ | ||
{ name="Shahobiddin Anorboyev", email="[email protected]" }, | ||
] | ||
maintainers = [ | ||
{ name="Shahobiddin Anorboyev", email="[email protected]" }, | ||
] | ||
keywords = ["telegram", "bot", "api", "library", "small"] | ||
dependencies = [ | ||
"requests", | ||
] | ||
description = "small api library for telegram bot api" | ||
readme = "README.md" | ||
requires-python = ">=3.8" | ||
classifiers = [ | ||
"Programming Language :: Python :: 3", | ||
"License :: OSI Approved :: MIT License", | ||
"Operating System :: OS Independent", | ||
] | ||
|
||
[project.urls] | ||
Homepage = "https://github.com/anorprogrammer/picogram" | ||
Issues = "https://github.com/anorprogrammer/picogram/issues" | ||
|
||
|