Telegram bot
Project description
Telegram Bot
Send data through the telegram bot API. Currently, supports sending messages and photos.
Installation
Install with pip
pip install ciresbot
Usage
Python Module
Creating a bot requires only its token. To send messages or photos it needs to know the chat id of the recipient(s) (see below how to obtain the chat ids).
from telegrambot import TelegramBot
bot = TelegramBot("MyBotToken")
bot.send_message("Hello World", chat_id="12345")
bot.send_photo("./dog.jpg", caption="Cute dog", chat_id="12345")
Ideally, the token bot should not be hardcoded, the module also comes with some utilities to read your tokens from a file. It expects a csv file with the following format:
# tokens.csv
botName,token
MyBot,MyBotToken12345
Also, you can read your chats from a csv file:
chatName,chatId,bot
MyChat,12345,MyBot
If you have a file with the above specifications you can use the utility functions as in the following example:
from telegrambot import TelegramBot
from telegrambot.utils import read_token_file, get_chat_id
token = read_token_file("./tokens.csv", "MyBot")
if not token:
raise ValueError("Token not found")
chat_id = get_chat_id("MyChat", "./chats.csv")
if not chat_id:
raise ValueError("Chat id not found")
bot = TelegramBot(token)
success, status_code, message = bot.send_message("Hello", chat_id)
if success:
print(f"Message sent successfully: {message}")
else:
print(f"Failed to send message {status_code}")
Command Line Tool
The command line tool supports the following modes:
message
: Send a text message through Telegram using a bot.photo
: Send a photo through Telegram using a bot.updates
: Get the latest updates of the bot from Telegram.
Options
Common options that can be used with all modes:
--bot, -b
: Name or token of the bot that will be used (required).--token, -t
: Whether a token was passed. If not, it is assumed that the bot name was passed.--bot-file, -f
: Name of the CSV file containing the token(s) of the bot(s). Required if the bot name was passed.--chat, -c
: Name or ID of the chat where the info will be sent (required).--id, -i
: Whether a chat ID was passed. If not, it is assumed that the chat name was passed.--chat-file, -cf
: Name of the CSV file containing the ID(s) of the chat(s). Required if the bot name was passed.
Specific options for each mode:
Message Mode
--message, -m
: Text of the message (required).
Photo Mode
--photo, -p
: Path to the photo file (required).--caption, -cp
: A caption for the photo (optional).
Examples
Sending a message using a bot:
telegrambot message --bot MyBot --message "Hello, World!" --chat MyChat --bot-file ./bot.csv --chat-file ./chat.csv
Sending a photo using a bot:
telegrambot photo --bot MyBot --photo ./photo.jpg --caption "A picture" --chat MyChat --bot-file ./bot.csv --chat-file ./chat.csv
Getting updates of a bot
telegrambot updates --bot MyBot --bot-file ./bot.csv
Obtaining chat ids
To obtain the chat id, first add the bot to a new chat or an existing one. Then send a message to the chat. Wait a few seconds and after that get the updates of the bot, you can use the CLI. In the updates the chat id will appear.
License
Created by Daniel Ibarrola. It is licensed under the terms of the MIT license.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file ciresbot-0.2.4.tar.gz
.
File metadata
- Download URL: ciresbot-0.2.4.tar.gz
- Upload date:
- Size: 6.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.9.18
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8db91046469fe8414e33693b55b6dbed1d9ccc03edbd0923c71bf737d0a7acfe |
|
MD5 | d87c922ad5fcc3550d20a3e8758037da |
|
BLAKE2b-256 | 92a842dd6f9586d0c761d1508da659b8e9e41f750cfe72187f8caf83f30da36b |
File details
Details for the file ciresbot-0.2.4-py3-none-any.whl
.
File metadata
- Download URL: ciresbot-0.2.4-py3-none-any.whl
- Upload date:
- Size: 7.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.9.18
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | e99f812327c51abab2d52af02cb576175ea517e0ca9aeeb7acdb8feb218113c4 |
|
MD5 | 5e4acea5da1f51151d29efeee617c7d4 |
|
BLAKE2b-256 | 4f465706f8fca155111d7b616bc6f7288af7ca37b2cecba48b60792f5f8bdb8e |