2015-06-26 09:55:13 +03:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
import requests
|
|
|
|
|
2015-06-30 06:49:35 +03:00
|
|
|
import telebot
|
2015-06-30 08:20:44 +03:00
|
|
|
from telebot import types
|
2015-06-30 06:49:35 +03:00
|
|
|
|
2015-06-26 17:35:52 +03:00
|
|
|
|
2015-06-26 10:46:02 +03:00
|
|
|
def get_me(token):
|
2015-06-26 17:35:52 +03:00
|
|
|
api_url = telebot.API_URL
|
2015-06-26 10:46:02 +03:00
|
|
|
method_url = r'getMe'
|
2015-06-26 17:35:52 +03:00
|
|
|
request_url = api_url + 'bot' + token + '/' + method_url
|
2015-06-26 10:46:02 +03:00
|
|
|
req = requests.get(request_url)
|
2015-06-27 16:55:45 +03:00
|
|
|
return check_result(method_url, req)
|
2015-06-26 09:55:13 +03:00
|
|
|
|
2015-06-26 17:35:52 +03:00
|
|
|
|
2015-06-26 09:55:13 +03:00
|
|
|
def send_message(token, chat_id, text, disable_web_page_preview=None, reply_to_message_id=None, reply_markup=None):
|
2015-06-26 10:46:02 +03:00
|
|
|
"""
|
|
|
|
Use this method to send text messages. On success, the sent Message is returned.
|
|
|
|
:param token:
|
|
|
|
:param chat_id:
|
|
|
|
:param text:
|
|
|
|
:param disable_web_page_preview:
|
|
|
|
:param reply_to_message_id:
|
|
|
|
:param reply_markup:
|
|
|
|
:return:
|
|
|
|
"""
|
2015-06-26 09:55:13 +03:00
|
|
|
api_url = telebot.API_URL
|
|
|
|
method_url = r'sendMessage'
|
|
|
|
request_url = api_url + 'bot' + token + '/' + method_url
|
2015-06-26 10:19:05 +03:00
|
|
|
payload = {'chat_id': str(chat_id), 'text': text}
|
2015-06-26 09:55:13 +03:00
|
|
|
if disable_web_page_preview:
|
2015-06-26 10:19:05 +03:00
|
|
|
payload['disable_web_page_preview'] = disable_web_page_preview
|
2015-06-26 09:55:13 +03:00
|
|
|
if reply_to_message_id:
|
2015-06-26 10:19:05 +03:00
|
|
|
payload['reply_to_message_id'] = reply_to_message_id
|
2015-06-26 09:55:13 +03:00
|
|
|
if reply_markup:
|
2015-06-30 08:20:44 +03:00
|
|
|
payload['reply_markup'] = convert_markup(reply_markup)
|
2015-06-26 10:19:05 +03:00
|
|
|
req = requests.get(request_url, params=payload)
|
2015-06-27 16:55:45 +03:00
|
|
|
return check_result(method_url, req)
|
2015-06-26 13:02:30 +03:00
|
|
|
|
2015-06-26 17:35:52 +03:00
|
|
|
|
2015-06-30 02:44:14 +03:00
|
|
|
def get_updates(token, offset=None):
|
2015-06-26 13:02:30 +03:00
|
|
|
api_url = telebot.API_URL
|
|
|
|
method_url = r'getUpdates'
|
2015-06-30 02:44:14 +03:00
|
|
|
if offset is not None:
|
|
|
|
request_url = api_url + 'bot' + token + '/' + method_url + '?offset=' + str(offset)
|
|
|
|
else:
|
|
|
|
request_url = api_url + 'bot' + token + '/' + method_url
|
2015-06-26 13:02:30 +03:00
|
|
|
req = requests.get(request_url)
|
2015-06-27 17:11:18 +03:00
|
|
|
return check_result(method_url, req)
|
2015-06-26 17:16:11 +03:00
|
|
|
|
2015-06-26 17:35:52 +03:00
|
|
|
|
|
|
|
def forward_message(token, chat_id, from_chat_id, message_id):
|
2015-06-26 17:16:11 +03:00
|
|
|
api_url = telebot.API_URL
|
|
|
|
method_url = r'forwardMessage'
|
2015-06-26 17:35:52 +03:00
|
|
|
request_url = api_url + 'bot' + token + '/' + method_url
|
|
|
|
payload = {'chat_id': chat_id, 'from_chat_id': from_chat_id, 'message_id': message_id}
|
|
|
|
req = requests.get(request_url, params=payload)
|
2015-06-27 16:55:45 +03:00
|
|
|
return check_result(method_url, req)
|
2015-06-26 20:53:07 +03:00
|
|
|
|
|
|
|
|
|
|
|
def send_photo(token, chat_id, photo, caption=None, reply_to_message_id=None, reply_markup=None):
|
|
|
|
api_url = telebot.API_URL
|
|
|
|
method_url = r'sendPhoto'
|
|
|
|
request_url = api_url + 'bot' + token + '/' + method_url
|
|
|
|
payload = {'chat_id': chat_id}
|
|
|
|
files = {'photo': photo}
|
|
|
|
if caption:
|
|
|
|
payload['caption'] = caption
|
|
|
|
if reply_to_message_id:
|
|
|
|
payload['reply_to_message_id'] = reply_to_message_id
|
|
|
|
if reply_markup:
|
2015-06-30 08:20:44 +03:00
|
|
|
payload['reply_markup'] = convert_markup(reply_markup)
|
2015-06-26 20:53:07 +03:00
|
|
|
req = requests.post(request_url, params=payload, files=files)
|
2015-06-27 16:55:45 +03:00
|
|
|
return check_result(method_url, req)
|
|
|
|
|
2015-06-26 20:53:07 +03:00
|
|
|
|
2015-06-27 17:11:18 +03:00
|
|
|
def send_location(token, chat_id, latitude, longitude, reply_to_message_id=None, reply_markup=None):
|
|
|
|
api_url = telebot.API_URL
|
|
|
|
method_url = r'sendLocation'
|
|
|
|
request_url = api_url + 'bot' + token + '/' + method_url
|
|
|
|
payload = {'chat_id': chat_id, 'latitude': latitude, 'longitude': longitude}
|
|
|
|
if reply_to_message_id:
|
|
|
|
payload['reply_to_message_id'] = reply_to_message_id
|
|
|
|
if reply_markup:
|
2015-06-30 08:20:44 +03:00
|
|
|
payload['reply_markup'] = convert_markup(reply_markup)
|
2015-06-27 17:11:18 +03:00
|
|
|
req = requests.get(request_url, params=payload)
|
|
|
|
return check_result(method_url, req)
|
|
|
|
|
2015-06-30 06:54:04 +03:00
|
|
|
|
|
|
|
def send_chat_action(token, chat_id, action):
|
2015-06-28 12:56:32 +03:00
|
|
|
api_url = telebot.API_URL
|
|
|
|
method_url = r'sendChatAction'
|
|
|
|
request_url = api_url + 'bot' + token + '/' + method_url
|
|
|
|
payload = {'chat_id': chat_id, 'action': action}
|
|
|
|
req = requests.get(request_url, params=payload)
|
|
|
|
return check_result(method_url, req)
|
2015-06-27 17:11:18 +03:00
|
|
|
|
2015-06-30 06:54:04 +03:00
|
|
|
|
2015-06-26 21:14:45 +03:00
|
|
|
def send_data(token, chat_id, data, data_type, reply_to_message_id=None, reply_markup=None):
|
2015-06-26 20:53:07 +03:00
|
|
|
api_url = telebot.API_URL
|
2015-06-26 21:14:45 +03:00
|
|
|
method_url = get_method_by_type(data_type)
|
2015-06-26 20:53:07 +03:00
|
|
|
request_url = api_url + 'bot' + token + '/' + method_url
|
|
|
|
payload = {'chat_id': chat_id}
|
2015-06-26 21:14:45 +03:00
|
|
|
files = {data_type: data}
|
2015-06-26 20:53:07 +03:00
|
|
|
if reply_to_message_id:
|
|
|
|
payload['reply_to_message_id'] = reply_to_message_id
|
|
|
|
if reply_markup:
|
2015-06-30 08:20:44 +03:00
|
|
|
payload['reply_markup'] = convert_markup(reply_markup)
|
2015-06-26 20:53:07 +03:00
|
|
|
req = requests.post(request_url, params=payload, files=files)
|
2015-06-27 16:55:45 +03:00
|
|
|
return check_result(method_url, req)
|
|
|
|
|
2015-06-26 20:53:07 +03:00
|
|
|
|
2015-06-26 21:14:45 +03:00
|
|
|
def get_method_by_type(data_type):
|
|
|
|
if data_type == 'audio':
|
2015-06-26 20:53:07 +03:00
|
|
|
return 'sendAudio'
|
2015-06-26 21:14:45 +03:00
|
|
|
if data_type == 'document':
|
2015-06-26 20:53:07 +03:00
|
|
|
return 'sendDocument'
|
2015-06-26 21:14:45 +03:00
|
|
|
if data_type == 'sticker':
|
2015-06-26 20:53:07 +03:00
|
|
|
return 'sendSticker'
|
2015-06-26 21:14:45 +03:00
|
|
|
if data_type == 'video':
|
2015-06-26 20:53:07 +03:00
|
|
|
return 'sendVideo'
|
2015-06-27 16:55:45 +03:00
|
|
|
|
|
|
|
|
|
|
|
def check_result(func_name, result):
|
|
|
|
if result.status_code != 200:
|
|
|
|
raise ApiError(func_name + r' error.', result)
|
|
|
|
try:
|
|
|
|
result_json = result.json()
|
|
|
|
if not result_json['ok']:
|
2015-06-30 06:49:35 +03:00
|
|
|
raise Exception(func_name, ' failed, result=' + result_json)
|
2015-06-27 16:55:45 +03:00
|
|
|
except:
|
|
|
|
raise ApiError(func_name + r' error.', result)
|
|
|
|
return result_json
|
|
|
|
|
2015-07-01 05:36:58 +03:00
|
|
|
|
2015-06-30 08:20:44 +03:00
|
|
|
def convert_markup(markup):
|
|
|
|
if isinstance(markup, types.ReplyKeyboardMarkup):
|
|
|
|
return markup.to_json()
|
|
|
|
else:
|
|
|
|
return markup
|
2015-06-27 16:55:45 +03:00
|
|
|
|
2015-07-01 05:36:58 +03:00
|
|
|
|
2015-06-27 16:55:45 +03:00
|
|
|
class ApiError(Exception):
|
|
|
|
def __init__(self, message, result):
|
|
|
|
super(ApiError, self).__init__(message)
|
|
|
|
self.result = result
|