import requests
import json
# Set up the bot credentials
BOT_TOKEN = "your_bot_token_here"
BASE_URL = f"https://api.telegram.org/bot{BOT_TOKEN}/" # telegram as an example
# Define a function to send a message
def send_message(chat_id, text):
url = BASE_URL + "sendMessage"
data = {"chat_id": chat_id, "text": text}
response = requests.post(url, data=data)
return response.json()
# Define a function to handle incoming messages
def handle_message(message):
chat_id = message["chat"]["id"]
text = message["text"]
# Respond to the user's message
if "hello" in text.lower():
send_message(chat_id, "Hello, how are you?")
elif "how are you" in text.lower():
send_message(chat_id, "I'm doing well, thanks for asking!")
else:
send_message(chat_id, "Sorry, I didn't understand what you said.")
# Continuously poll for new messages
def main():
offset = 0
while True:
url = BASE_URL + "getUpdates"
params = {"offset": offset, "timeout": 30}
response = requests.get(url, params=params)
messages = response.json()["result"]
# Handle each message
for message in messages:
handle_message(message)
offset = message["update_id"] + 1
if __name__ == "__main__":
main()