mirror of
https://github.com/eternnoir/pyTelegramBotAPI.git
synced 2023-08-10 21:12:57 +03:00
Merge pull request #11 from pevdh/master
Implemented the offset parameter.
This commit is contained in:
commit
9e27680266
@ -1,11 +1,12 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
import apihelper
|
||||
import json
|
||||
import types
|
||||
import time
|
||||
import threading
|
||||
|
||||
import apihelper
|
||||
import types
|
||||
|
||||
"""
|
||||
Module : telebot
|
||||
"""
|
||||
@ -33,46 +34,27 @@ class TeleBot:
|
||||
|
||||
def __init__(self, token):
|
||||
self.token = token
|
||||
self.update_entries = {}
|
||||
self.update_listener = []
|
||||
self.chat_list = {}
|
||||
self.update_id_list = []
|
||||
self.max_message_size = 100000
|
||||
self.polling_thread = None
|
||||
self.__stop_polling = False
|
||||
self.interval = 3
|
||||
|
||||
def get_last_update_id(self):
|
||||
return self.update_id_list[-1] if len(self.update_id_list) > 0 else None
|
||||
self.last_update_id = 0
|
||||
|
||||
def get_update(self):
|
||||
result = apihelper.get_updates(self.token)
|
||||
if result['ok'] is not True:
|
||||
raise Exception('getMe Error.' + json.dumps(result))
|
||||
result = apihelper.get_updates(self.token, offset=(self.last_update_id + 1))
|
||||
updates = result['result']
|
||||
notify_updates = []
|
||||
for update in updates:
|
||||
if update['update_id'] in self.update_entries:
|
||||
continue
|
||||
if update['update_id'] > self.last_update_id:
|
||||
self.last_update_id = update['update_id']
|
||||
msg = types.Message.de_json(json.dumps(update['message']))
|
||||
self.__append_message_to_cache(update['update_id'], msg)
|
||||
notify_updates.append(msg)
|
||||
self.__notify_update(notify_updates)
|
||||
|
||||
def __append_message_to_cache(self, update_id, message):
|
||||
# over buffer size
|
||||
if len(self.update_id_list) > self.max_message_size:
|
||||
# remove oldest element.
|
||||
upid = self.update_id_list.pop()
|
||||
if upid in self.update_entries:
|
||||
del self.update_entries[upid]
|
||||
|
||||
self.update_entries[update_id] = message
|
||||
self.update_id_list.append(update_id)
|
||||
|
||||
def __notify_update(self, new_messages):
|
||||
for listener in self.update_listener:
|
||||
t = threading.Thread(target=listener, args=(new_messages))
|
||||
t = threading.Thread(target=listener, args=new_messages)
|
||||
t.start()
|
||||
|
||||
def polling(self, interval=3):
|
||||
|
@ -1,8 +1,9 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
import telebot
|
||||
import requests
|
||||
|
||||
import telebot
|
||||
|
||||
|
||||
def get_me(token):
|
||||
api_url = telebot.API_URL
|
||||
@ -85,6 +86,7 @@ def send_location(token, chat_id, latitude, longitude, reply_to_message_id=None,
|
||||
req = requests.get(request_url, params=payload)
|
||||
return check_result(method_url, req)
|
||||
|
||||
|
||||
def send_chat_action(token, chat_id, action):
|
||||
api_url = telebot.API_URL
|
||||
method_url = r'sendChatAction'
|
||||
@ -93,6 +95,7 @@ def send_chat_action(token,chat_id,action):
|
||||
req = requests.get(request_url, params=payload)
|
||||
return check_result(method_url, req)
|
||||
|
||||
|
||||
def send_data(token, chat_id, data, data_type, reply_to_message_id=None, reply_markup=None):
|
||||
api_url = telebot.API_URL
|
||||
method_url = get_method_by_type(data_type)
|
||||
@ -124,7 +127,7 @@ def check_result(func_name, result):
|
||||
try:
|
||||
result_json = result.json()
|
||||
if not result_json['ok']:
|
||||
raise Exception('')
|
||||
raise Exception(func_name, ' failed, result=' + result_json)
|
||||
except:
|
||||
raise ApiError(func_name + r' error.', result)
|
||||
return result_json
|
||||
|
@ -62,7 +62,7 @@ class Message:
|
||||
def de_json(cls, json_string):
|
||||
obj = json.loads(json_string)
|
||||
message_id = obj['message_id']
|
||||
fromUser = User.de_json(json.dumps(obj['from']))
|
||||
from_user = User.de_json(json.dumps(obj['from']))
|
||||
chat = Message.parse_chat(obj['chat'])
|
||||
date = obj['date']
|
||||
content_type = None
|
||||
@ -88,7 +88,7 @@ class Message:
|
||||
if 'location' in obj:
|
||||
opts['location'] = Location.de_json(json.dumps(obj['location']))
|
||||
content_type = 'location'
|
||||
return Message(message_id, fromUser, date, chat, content_type, opts)
|
||||
return Message(message_id, from_user, date, chat, content_type, opts)
|
||||
|
||||
@classmethod
|
||||
def parse_chat(cls, chat):
|
||||
@ -262,7 +262,7 @@ class UserProfilePhotos:
|
||||
|
||||
|
||||
class ReplyKeyboardMarkup:
|
||||
def __init__(self, keyboard=[], resize_keyboard=None, one_time_keyboard=None, selective=None, row_width=3):
|
||||
def __init__(self, resize_keyboard=None, one_time_keyboard=None, selective=None, row_width=3):
|
||||
self.resize_keyboard = resize_keyboard
|
||||
self.one_time_keyboard = one_time_keyboard
|
||||
self.selective = selective
|
||||
@ -307,15 +307,14 @@ class ReplyKeyboardMarkup:
|
||||
https://core.telegram.org/bots/api#replykeyboardmarkup
|
||||
:return:
|
||||
"""
|
||||
json_dict = {}
|
||||
json_dict['keyboard'] = self.keyboard
|
||||
if self.one_time_keyboard != False:
|
||||
json_dict = {'keyboard': self.keyboard}
|
||||
if self.one_time_keyboard:
|
||||
json_dict['one_time_keyboard'] = True
|
||||
|
||||
if self.resize_keyboard != False:
|
||||
if self.resize_keyboard:
|
||||
json_dict['resize_keyboard'] = True
|
||||
|
||||
if self.selective != False:
|
||||
if self.selective:
|
||||
json_dict['selective'] = True
|
||||
|
||||
return json.dumps(json_dict)
|
||||
|
@ -34,7 +34,7 @@ def test_json_GroupChat():
|
||||
def test_json_Document():
|
||||
json_string = r'{"file_name":"Text File","thumb":{},"file_id":"BQADBQADMwIAAsYifgZ_CEh0u682xwI","file_size":446}'
|
||||
doc = types.Document.de_json(json_string)
|
||||
assert doc.thumb == None
|
||||
assert doc.thumb is None
|
||||
assert doc.file_name == 'Text File'
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user