Skip to main content

Python package for interacting with YoPhone Bot API

Project description

YoPhoneBot

YoPhoneBot is a Python library for creating bots that interact with the YoPhone Messenger API. It allows you to handle messages, commands, and automate responses seamlessly.

Features

  • Simple setup for message and command handlers.
  • Polling-based update mechanism.
  • Base64 encoding/decoding support for messages.
  • Easy-to-use methods for sending and receiving messages.

Installation

Install YoPhoneBot from PyPI:

pip install yophonebot

Getting Started

Initialize Your Bot

from yophonebot.bot import YoPhoneBot

bot = YoPhoneBot("YOUR_API_KEY_HERE")

Handle Regular Messages

Register a handler for processing non-command messages:

@bot.message_handler
def handle_message(message):
    print(f"Received message from {message.sender.first_name}: {message.text}")
    bot.send_message(message.chat_id, "Hello!")

Handle Commands

Register a handler for specific commands like /start:

@bot.command_handler("/start")
def start_command(message):
    bot.send_message(message.chat_id, f"Welcome, {message.sender.first_name}!")

Start Polling

Begin polling for updates:

if __name__ == "__main__":
    bot.start_polling()

API Documentation

Class: YoPhoneBot

__init__(api_key: str, base_url: str = "https://yoai.yophone.com/api/pub")

Initialize the bot with your API key.

  • api_key: Your YoPhone API key.
  • base_url: Base URL for the YoPhone API (default provided).

message_handler(func: Callable[[Message], None])

Register a handler for processing regular messages.

  • func: Function to handle incoming messages. Receives a Message object.

command_handler(command: str)

Register a handler for specific commands.

  • command: The command string (e.g., "/start").

start_polling(interval: int = 2)

Start an infinite polling loop to fetch updates.

  • interval: Time in seconds between polling requests.

send_message(chat_id: str, text: str)

Send a message to a chat.

  • chat_id: The ID of the chat.
  • text: The message text to send.

Class: Message

Encapsulates the structure of a message.

Attributes:

  • update_id: Unique ID of the update.
  • bot_id: ID of the bot receiving the message.
  • chat_id: ID of the chat where the message originated.
  • text: The message text (decoded from Base64).
  • sender: A Sender object with:
    • first_name: Sender's first name.
    • last_name: Sender's last name.
    • id: Sender's ID.

Class: Sender

Encapsulates the structure of a sender.

Attributes:

  • first_name: Sender's first name.
  • last_name: Sender's last name.
  • id: Sender's ID.

Example Bot

Here’s an example bot that handles both regular messages and /start command:

from yophonebot.bot import YoPhoneBot

bot = YoPhoneBot("YOUR_API_KEY_HERE")

@bot.command_handler("/start")
def start_command(message):
    bot.send_message(message.chat_id, f"Welcome, {message.sender.first_name}!")

@bot.message_handler
def handle_message(message):
    bot.send_message(message.chat_id, f"You said: {message.text}")

if __name__ == "__main__":
    bot.start_polling()

Troubleshooting

  • Error: 'botId'

    • Ensure the getUpdates response contains all required fields.
    • Add logging to inspect the response structure.
  • Error: HTTPError: 400 Bad Request

    • Verify your API token.
    • Check the structure of your requests (e.g., sendMessage payload).

License

This project is licensed under the MIT License. See the LICENSE file for details.


Contributing

Contributions are welcome! Please submit a pull request or open an issue for discussion.


Acknowledgments

Special thanks to the YoPhone team for their API and documentation.

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

yophonebot-0.1.6.tar.gz (5.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

yophonebot-0.1.6-py3-none-any.whl (6.1 kB view details)

Uploaded Python 3

File details

Details for the file yophonebot-0.1.6.tar.gz.

File metadata

  • Download URL: yophonebot-0.1.6.tar.gz
  • Upload date:
  • Size: 5.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.12.3

File hashes

Hashes for yophonebot-0.1.6.tar.gz
Algorithm Hash digest
SHA256 f06496dc97038ee710af1e2814ab07955be90c0c05886828ed73c8ce993ece07
MD5 cc87a97410217cfd653c8eaeb29f949a
BLAKE2b-256 00535a57731420adc23492a3be0b7cbe389037dcabf42697041fae45c22fe7dc

See more details on using hashes here.

File details

Details for the file yophonebot-0.1.6-py3-none-any.whl.

File metadata

  • Download URL: yophonebot-0.1.6-py3-none-any.whl
  • Upload date:
  • Size: 6.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.12.3

File hashes

Hashes for yophonebot-0.1.6-py3-none-any.whl
Algorithm Hash digest
SHA256 5e40bd14668a3bf09f7ff060520f21a208cc78a30b737c024693ac9236854db6
MD5 fd67193636f99682fc9b179d7c894d69
BLAKE2b-256 4ce253b9fbfc43b456c967c3a483281d6a176c46b9caa988b1e07aa6fef83e65

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page