From 1227939e667c05341a977e33fefffaba5dbd4d06 Mon Sep 17 00:00:00 2001 From: Mahdiali313 <52355596+Mahdiali313@users.noreply.github.com> Date: Fri, 26 Mar 2021 16:36:10 +0430 Subject: [PATCH 01/23] fix MultipartEncoder error https://toolbelt.readthedocs.io/en/latest/user.html --- client.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client.py b/client.py index 90f04a2..8b5f543 100644 --- a/client.py +++ b/client.py @@ -3,7 +3,7 @@ import json import os from time import sleep - +from requests_toolbelt import MultipartEncoder class Client: HEADERS = {'Content-Type': 'Application/json', 'Accept': 'Application/json'} From b221cb38c2e1b9481a93c8d0ad9746e6c33e345c Mon Sep 17 00:00:00 2001 From: Mahdiali313 <52355596+Mahdiali313@users.noreply.github.com> Date: Fri, 26 Mar 2021 16:37:11 +0430 Subject: [PATCH 02/23] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 7aae189..ceab72e 100644 --- a/README.md +++ b/README.md @@ -6,6 +6,7 @@ Soroush Messenger Bot Wrapper for Python - Python 2.7+ - requests - sseclient-py +- requests_toolbelt ## Installation ## Run the below commands From bce71197f05bb22d7c61bff2816c29fc43ed7804 Mon Sep 17 00:00:00 2001 From: Mahdiali313 <52355596+Mahdiali313@users.noreply.github.com> Date: Fri, 26 Mar 2021 16:37:28 +0430 Subject: [PATCH 03/23] Update requirements.txt --- requirements.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/requirements.txt b/requirements.txt index a944a1c..8f1ef22 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,2 +1,3 @@ requests sseclient-py +requests_toolbelt From 85473cceabeb0f234b1d73a6f7fad9147117f86a Mon Sep 17 00:00:00 2001 From: Mahdiali313 <52355596+Mahdiali313@users.noreply.github.com> Date: Sat, 24 Apr 2021 19:22:53 +0430 Subject: [PATCH 04/23] new way to use Soroush Bot easier --- SoroushBot.py | 90 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 SoroushBot.py diff --git a/SoroushBot.py b/SoroushBot.py new file mode 100644 index 0000000..a20d4b3 --- /dev/null +++ b/SoroushBot.py @@ -0,0 +1,90 @@ +from client import Client +import os +from os.path import getsize +import ntpath +from moviepy.editor import VideoFileClip + +class SoroushBot: + + def __init__(self, bot_token): + global b + b= Client(bot_token) + + def sendText(self,target_id,caption): + resualt=b.send_text(target_id,caption) + #resualt=[error,success] + return resualt + + def imageVideoResalotion(self,path): + clip=VideoFileClip(path) + width = int(round(clip.w)) + height = int(round(clip.h)) + return [width,height] + + def sendImage(self,target_id,image_path,image_thumbnail_path,caption): + rImage=b.upload_file(image_path) + #rImage=[image_error, image_url] + + if rImage[1]: + rThumb = b.upload_file(image_thumbnail_path) + #rThumb=[thumbnail_error, thumbnail_url] + scale=self.imageVideoResalotion(image_path) + + resualt = b.send_image(target_id, rImage[1], + ntpath.basename(image_path), + getsize(image_path), scale[0], scale[1], + rThumb[1], + caption) + #if(resualt[1] != 'OK'): + # print("paramets that gotten this function",image_path,image_thumbnail_path,caption) + return resualt + + else : + #if(rImage[1] != 'OK'): + # print("paramets entered for this function",image_path,image_thumbnail_path,caption) + return rImage + + + def sendVideo(self,target_id,video_path,video_thumbnail_path,caption,durationSeconds=None): + rVideo=b.upload_file(video_path) + #rVideo=[video_error, video_url] + + if rVideo[1]: + rThumb= b.upload_file(video_thumbnail_path) + #rThumb=[thumbnail_error, thumbnail_url] + + if (durationSeconds==None): + clip=VideoFileClip(video_path) + durationSeconds=int(round(clip.duration)) + + scale=self.imageVideoResalotion(video_path)#[width,height] + + resualt= b.send_video(target_id, rVideo[1], + ntpath.basename(video_path), + getsize(video_path), + durationSeconds*1000, scale[0], scale[1], + rThumb[1], + caption) + #if(resualt[1] != 'OK'): + # print("paramets that gotten this func",video_path,video_thumbnail_path,caption,durationSeconds) + return resualt + + else: + #if(rVideo[1] != 'OK'): + # print("paramets entered for this func",video_path,video_thumbnail_path,caption,durationSeconds) + return rVideo + + + def sendFile(self,target_id,file_path,caption): + rFile = b.upload_file(file_path) + #rFile=[error, file_url] + + resualt = b.send_attachment(target_id, rFile[1], ntpath.basename(file_path), + getsize(file_path), + caption) + return resualt + + def sendLocation(self,target_id, latitude, longitude, caption='', keyboard=None): + resualt=b.send_location(target_id, latitude, longitude, caption, keyboard) + + return resualt From f0b9a7f0b537d2581ba818bef031e86e0ad7af64 Mon Sep 17 00:00:00 2001 From: Mahdiali313 <52355596+Mahdiali313@users.noreply.github.com> Date: Sat, 24 Apr 2021 19:25:33 +0430 Subject: [PATCH 05/23] use moviepy for getting duration for video --- requirements.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/requirements.txt b/requirements.txt index 8f1ef22..28f210f 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,4 @@ requests sseclient-py requests_toolbelt +moviepy From 040bee16ea4b417bd1db97c22e52fc3ffbf3872d Mon Sep 17 00:00:00 2001 From: Mahdiali313 <52355596+Mahdiali313@users.noreply.github.com> Date: Sat, 24 Apr 2021 19:31:34 +0430 Subject: [PATCH 06/23] Update README.md --- README.md | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index ceab72e..bcd13e0 100644 --- a/README.md +++ b/README.md @@ -2,12 +2,6 @@ # Soroush Messenger Bot Python SDK Soroush Messenger Bot Wrapper for Python -## Dependencies ## -- Python 2.7+ -- requests -- sseclient-py -- requests_toolbelt - ## Installation ## Run the below commands ```bash @@ -19,11 +13,11 @@ pip install -r requirements.txt ## Usage ## ```python -from client import Client +from SoroushBot import SoroushBot bot_token = 'your bot token' -bot = Client(bot_token) +bot = SoroushBot(bot_token) try: to = 'user chat_id' @@ -42,6 +36,7 @@ except Exception as e: ``` "to" value in above example is chat_id of a bot user. You can find it in front of 'from' key in a message that user has sent to your bot. You can see more examples in the [examples](https://github.com/soroush-app/bot-python-sdk/tree/master/examples) directory. +use [SoroushBot](https://github.com/Mahdiali313/bot-python-sdk/blob/master/SoroushBot.py) function becuase it's easier to use! ## Contribute ## Contributions to the package are always welcome! From 7483e67d35d52347702edb0564b2e38b3b6f0ea1 Mon Sep 17 00:00:00 2001 From: Mahdiali313 <52355596+Mahdiali313@users.noreply.github.com> Date: Sun, 25 Apr 2021 12:01:45 +0430 Subject: [PATCH 07/23] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index bcd13e0..b8068d9 100644 --- a/README.md +++ b/README.md @@ -22,7 +22,7 @@ bot = SoroushBot(bot_token) try: to = 'user chat_id' - [error, success] = bot.send_text(to, 'Your text') + [error, success] = bot.sendText(to, 'Your text') if success: print('Message sent successfully') From c2e2d99596c2066181606555beb5850aa2a8feb3 Mon Sep 17 00:00:00 2001 From: Mahdiali313 <52355596+Mahdiali313@users.noreply.github.com> Date: Sun, 25 Apr 2021 12:05:39 +0430 Subject: [PATCH 08/23] Update README.md --- README.md | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index b8068d9..43f7e57 100644 --- a/README.md +++ b/README.md @@ -19,18 +19,13 @@ bot_token = 'your bot token' bot = SoroushBot(bot_token) -try: - to = 'user chat_id' +to = 'user chat_id' - [error, success] = bot.sendText(to, 'Your text') +[error, success] = bot.sendText(to, 'Your text') - if success: - print('Message sent successfully') - else: - print('Sending message failed: {}' .format(error)) +if success: + print('Message sent successfully') -except Exception as e: - print(e.args[0]) ``` From 8b6f6dafc5794316c9e38d69baab1deeff047408 Mon Sep 17 00:00:00 2001 From: Mahdiali313 <52355596+Mahdiali313@users.noreply.github.com> Date: Sun, 25 Apr 2021 12:16:37 +0430 Subject: [PATCH 09/23] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 43f7e57..13d0676 100644 --- a/README.md +++ b/README.md @@ -31,9 +31,9 @@ if success: ``` "to" value in above example is chat_id of a bot user. You can find it in front of 'from' key in a message that user has sent to your bot. You can see more examples in the [examples](https://github.com/soroush-app/bot-python-sdk/tree/master/examples) directory. + use [SoroushBot](https://github.com/Mahdiali313/bot-python-sdk/blob/master/SoroushBot.py) function becuase it's easier to use! ## Contribute ## Contributions to the package are always welcome! - Report any idea, bugs or issues you find on the [issue tracker](https://github.com/soroush-app/bot-python-sdk/issues). - - You can grab the source code at the package's [Git repository](https://github.com/soroush-app/bot-python-sdk.git). From 0c4fd6ca70b6c3463a64945cebe7d33f48e9e886 Mon Sep 17 00:00:00 2001 From: Mahdiali313 <52355596+Mahdiali313@users.noreply.github.com> Date: Sun, 25 Apr 2021 21:17:17 +0430 Subject: [PATCH 10/23] Update and rename send_attachment.py to send_file.py --- examples/send_attachment.py | 31 ------------------------------- examples/send_file.py | 18 ++++++++++++++++++ 2 files changed, 18 insertions(+), 31 deletions(-) delete mode 100644 examples/send_attachment.py create mode 100644 examples/send_file.py diff --git a/examples/send_attachment.py b/examples/send_attachment.py deleted file mode 100644 index 7710ae8..0000000 --- a/examples/send_attachment.py +++ /dev/null @@ -1,31 +0,0 @@ -from sys import path -path.append('..') -from client import Client -from os.path import getsize -import ntpath - -bot_token = 'your bot token' - -bot = Client(bot_token) - -try: - to = 'user chat_id' - file_path = 'your file path' - - [error, file_url] = bot.upload_file(file_path) - - if error: - print('error in uploading file: {}' .format(error)) - else: - print('file uploaded successfully with url: {}' .format(file_url)) - - [error, success] = bot.send_attachment(to, file_url, ntpath.basename(file_path), getsize(file_path), - caption='your caption') - - if success: - print('Message sent successfully') - else: - print('Sending message failed: {}' .format(error)) - -except Exception as e: - print(e.args[0]) diff --git a/examples/send_file.py b/examples/send_file.py new file mode 100644 index 0000000..42f43b7 --- /dev/null +++ b/examples/send_file.py @@ -0,0 +1,18 @@ +from sys import path +path.append('..') +from SoroushBot import SoroushBot + +bot_token = 'your bot token' +bot = SoroushBot(bot_token) + +to = 'user chat_id' +path = 'your file path' + +[error, success] = bot.sendFile( + target_id = to, + file_path = path, + caption='Your Caption' + ) + +if error: + print('error in sending file: {}' .format(error)) From 03ca9fbf42545c82237fef06f93da936f1ded4c2 Mon Sep 17 00:00:00 2001 From: Mahdiali313 <52355596+Mahdiali313@users.noreply.github.com> Date: Sun, 25 Apr 2021 21:21:38 +0430 Subject: [PATCH 11/23] Update send_video.py --- examples/send_video.py | 47 ++++++++++++------------------------------ 1 file changed, 13 insertions(+), 34 deletions(-) diff --git a/examples/send_video.py b/examples/send_video.py index e7a7769..3042fd4 100644 --- a/examples/send_video.py +++ b/examples/send_video.py @@ -1,41 +1,20 @@ from sys import path path.append('..') -from client import Client -from os.path import getsize -import ntpath +from SoroushBot import SoroushBot bot_token = 'your bot token' +bot = SoroushBot(bot_token) -bot = Client(bot_token) +to = 'user chat_id' +path_video = 'your video path' +path_video_thumbnail = 'your thumbnail of video path' -try: - to = 'user chat_id' - video_path = 'your video path' - video_thumbnail_path = 'video thumbnail path' - video_duration_in_milliseconds = 7000 +[error, success] = bot.sendVideo( + target_id = to, + video_path = path_video, + video_thumbnail_path = path_video_thumbnail , + caption='Your Caption' + ) - [video_error, video_url] = bot.upload_file(video_path) - if video_error: - print('error in uploading video: {}' .format(video_url)) - else: - print('video uploaded successfully with url: {}' .format(video_url)) - - if video_url: - [thumbnail_error, thumbnail_url] = bot.upload_file(video_thumbnail_path) - if thumbnail_error: - print('error in uploading thumbnail: {}' .format(thumbnail_error)) - else: - print('thumbnail uploaded successfully with url: {}' .format(thumbnail_url)) - - [error, success] = bot.send_video(to, video_url, ntpath.basename(video_path), getsize(video_path), - video_duration_in_milliseconds, 512, 512, - thumbnail_url, - caption='your caption') - - if success: - print('Message sent successfully') - else: - print('Sending message failed: {}' .format(error)) - -except Exception as e: - print(e.args[0]) +if error: + print('error in sending video: {}' .format(error)) From 8a6c19c43b802a2e291ebdcec466bf4f0ea2ae59 Mon Sep 17 00:00:00 2001 From: Mahdiali313 <52355596+Mahdiali313@users.noreply.github.com> Date: Sun, 25 Apr 2021 21:23:43 +0430 Subject: [PATCH 12/23] Update send_location.py --- examples/send_location.py | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/examples/send_location.py b/examples/send_location.py index a9d54ea..a7b426b 100644 --- a/examples/send_location.py +++ b/examples/send_location.py @@ -1,20 +1,19 @@ from sys import path path.append('..') -from client import Client +from SoroushBot import SoroushBot bot_token = 'your bot token' +bot = SoroushBot(bot_token) -bot = Client(bot_token) +to = 'user chat_id' -try: - to = 'user chat_id' +[error, success] = bot.sendLocation( + target_id = to, + latitude = 35.7448416, + longitude = 51.3753212 , + caption='Your Caption' + ) - [error, success] = bot.send_location(to, 35.7448416, 51.3753212) +if error: + print('error in sending video: {}' .format(error)) - if success: - print('Message sent successfully') - else: - print('Sending message failed: {}' .format(error)) - -except Exception as e: - print(e.args[0]) From e10253fff42516936c49ec1fd0c4348d8d35723a Mon Sep 17 00:00:00 2001 From: Mahdiali313 <52355596+Mahdiali313@users.noreply.github.com> Date: Sun, 25 Apr 2021 21:25:31 +0430 Subject: [PATCH 13/23] Update send_location.py --- examples/send_location.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/send_location.py b/examples/send_location.py index a7b426b..c8d728c 100644 --- a/examples/send_location.py +++ b/examples/send_location.py @@ -15,5 +15,5 @@ ) if error: - print('error in sending video: {}' .format(error)) + print('error in sending Location: {}' .format(error)) From 1c41444bcb9e8b4ce559fe798c014f718932f939 Mon Sep 17 00:00:00 2001 From: Mahdiali313 <52355596+Mahdiali313@users.noreply.github.com> Date: Sun, 25 Apr 2021 21:27:50 +0430 Subject: [PATCH 14/23] Update send_image.py --- examples/send_image.py | 45 ++++++++++++------------------------------ 1 file changed, 13 insertions(+), 32 deletions(-) diff --git a/examples/send_image.py b/examples/send_image.py index 964f80c..048e5ad 100644 --- a/examples/send_image.py +++ b/examples/send_image.py @@ -1,39 +1,20 @@ from sys import path path.append('..') -from client import Client -from os.path import getsize -import ntpath +from SoroushBot import SoroushBot bot_token = 'your bot token' +bot = SoroushBot(bot_token) -bot = Client(bot_token) +to = 'user chat_id' +path_image = 'image path' +path_image_thumbnail = 'thumbnail path' -try: - to = 'user chat_id' - image_path = 'image path' - image_thumbnail_path = 'thumbnail path' +[error, success] = bot.sendVideo( + target_id = to, + image_path = path_image, + image_thumbnail_path = path_image_thumbnail , + caption='Your Caption' + ) - [image_error, image_url] = bot.upload_file(image_path) - if image_error: - print('error in uploading image: {}' .format(image_error)) - else: - print('image uploaded successfully with url: {}' .format(image_url)) - - if image_url: - [thumbnail_error, thumbnail_url] = bot.upload_file(image_thumbnail_path) - if thumbnail_error: - print('error in uploading thumbnail: {}' .format(thumbnail_error)) - else: - print('thumbnail uploaded successfully with url: {}' .format(thumbnail_url)) - - [error, success] = bot.send_image(to, image_url, ntpath.basename(image_path), getsize(image_path), 512, 512, - thumbnail_url, - caption='your caption') - - if success: - print('Message sent successfully') - else: - print('Sending message failed: {}' .format(error)) - -except Exception as e: - print(e.args[0]) +if error: + print('error in sending image: {}' .format(error)) From 7a45e1985c1f8b892226398bd11c56bfa1358129 Mon Sep 17 00:00:00 2001 From: Mahdiali313 <52355596+Mahdiali313@users.noreply.github.com> Date: Sun, 25 Apr 2021 21:28:12 +0430 Subject: [PATCH 15/23] Update send_image.py --- examples/send_image.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/send_image.py b/examples/send_image.py index 048e5ad..e649ba0 100644 --- a/examples/send_image.py +++ b/examples/send_image.py @@ -9,7 +9,7 @@ path_image = 'image path' path_image_thumbnail = 'thumbnail path' -[error, success] = bot.sendVideo( +[error, success] = bot.sendImage( target_id = to, image_path = path_image, image_thumbnail_path = path_image_thumbnail , From 81ce4fa352cd6938861efb2343654247d26b65db Mon Sep 17 00:00:00 2001 From: Mahdiali313 <52355596+Mahdiali313@users.noreply.github.com> Date: Sun, 25 Apr 2021 21:30:16 +0430 Subject: [PATCH 16/23] Update send_text.py --- examples/send_text.py | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/examples/send_text.py b/examples/send_text.py index 0ce16b3..19914bf 100644 --- a/examples/send_text.py +++ b/examples/send_text.py @@ -1,20 +1,14 @@ from sys import path path.append('..') -from client import Client +from SoroushBot import SoroushBot bot_token = 'your bot token' -bot = Client(bot_token) +bot = SoroushBot(bot_token) -try: - to = 'user chat_id' +to = 'user chat_id' - [error, success] = bot.send_text(to, 'Your text') +[error, success] = bot.sendText(to, 'Your text') - if success: - print('Message sent successfully') - else: - print('Sending message failed: {}' .format(error)) - -except Exception as e: - print(e.args[0]) +if error: + print('Sending message failed: {}' .format(error)) From 9c6a86fb2adb530c3564d902814e8cbb0ca0857d Mon Sep 17 00:00:00 2001 From: Mahdiali313 <52355596+Mahdiali313@users.noreply.github.com> Date: Sun, 25 Apr 2021 21:48:36 +0430 Subject: [PATCH 17/23] Update README.md --- README.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 13d0676..996964f 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,9 @@ # Soroush Messenger Bot Python SDK -Soroush Messenger Bot Wrapper for Python + +Use SoroushBot.py to send video ,image and file easier ! + +if you can ,please help us to complete this script (SoroushBot.py) . ## Installation ## Run the below commands From 3c8c3fe1406079e5bb4948ece9dfa0ba3af30809 Mon Sep 17 00:00:00 2001 From: Mahdiali313 <52355596+Mahdiali313@users.noreply.github.com> Date: Wed, 5 May 2021 08:39:02 +0430 Subject: [PATCH 18/23] Update client.py --- client.py | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/client.py b/client.py index 8b5f543..d759282 100644 --- a/client.py +++ b/client.py @@ -325,11 +325,16 @@ def download_file(self, file_url, save_file_path): def upload_file(self, file_path): if not os.path.isfile(file_path): - raise ValueError('Invalid file') - - try: - file = {'file': open(file_path, 'rb')} - response = requests.post(self.get_upload_file_url(), files=file) + raise ValueError('Invalid file (file with this name not exit or maybe remove or incorrect path you given)') + try: + session = requests.Session() + with open(file_path, "rb") as f: + m = MultipartEncoder({ + 'file':(file_path.split('/')[-1],f,"application/octet-stream") + }) + headers = {"Content-Type": m.content_type} + response = session.post(self.get_upload_file_url(),headers=headers, data=m) + session.close() if response.status_code == 200: if response: From eebe306e6f05176049684f2001a66f136d0199d7 Mon Sep 17 00:00:00 2001 From: Mahdiali313 <52355596+Mahdiali313@users.noreply.github.com> Date: Wed, 5 May 2021 08:40:21 +0430 Subject: [PATCH 19/23] send Audio added --- SoroushBot.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/SoroushBot.py b/SoroushBot.py index a20d4b3..ac12146 100644 --- a/SoroushBot.py +++ b/SoroushBot.py @@ -88,3 +88,18 @@ def sendLocation(self,target_id, latitude, longitude, caption='', keyboard=None) resualt=b.send_location(target_id, latitude, longitude, caption, keyboard) return resualt + + def sendAudio (self, target_id, audio_path, caption, audio_duration): + audioUrl=b.upload_file(audio_path) + + audiotype = 'PUSH_TO_TALK' + extra_params = { + 'fileDuration': audio_duration*1000 + } + + resualt = b.send_file(target_id, caption, + ntpath.basename(audio_path), audiotype, + audioUrl[1], getsize(audio_path), + extra_params) + + return resualt From ea60e3bb4afa8d0d662f21f3be87983509770643 Mon Sep 17 00:00:00 2001 From: Mahdiali313 <52355596+Mahdiali313@users.noreply.github.com> Date: Wed, 12 May 2021 14:24:57 +0430 Subject: [PATCH 20/23] send text to channel --- client.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/client.py b/client.py index d759282..5256c39 100644 --- a/client.py +++ b/client.py @@ -92,13 +92,16 @@ def send_message(self, post_data): except Exception as e: return [e.args[0], False] - def send_text(self, to, text, keyboard=None): - + def send_text(self, to, text,sendTochannel=0, keyboard=None): + '''if you set sendTochannel =1 ,it will send message to channel + (give id of channel without @ )''' post_data = { 'type': 'TEXT', 'to': to, 'body': text, } + if sendTochannel == 1 : + post_data['majorType']="CHANNEL" if keyboard is not None: post_data['keyboard'] = keyboard return self.send_message(post_data) From 530605edc8ba2c4b1a3fa82fdc1159933d3f2fb2 Mon Sep 17 00:00:00 2001 From: Mahdiali313 <52355596+Mahdiali313@users.noreply.github.com> Date: Wed, 12 May 2021 15:05:54 +0430 Subject: [PATCH 21/23] ability to send data to channel added --- client.py | 76 ++++++++++++++++++++++++++++++++++++------------------- 1 file changed, 50 insertions(+), 26 deletions(-) diff --git a/client.py b/client.py index 5256c39..c8b902a 100644 --- a/client.py +++ b/client.py @@ -92,21 +92,23 @@ def send_message(self, post_data): except Exception as e: return [e.args[0], False] - def send_text(self, to, text,sendTochannel=0, keyboard=None): - '''if you set sendTochannel =1 ,it will send message to channel - (give id of channel without @ )''' + def send_text(self, to, text,sendTochannel=False, keyboard=None): + '''if you set sendTochannel =True ,it will send message to channel + (to=id of channel without @ )''' post_data = { 'type': 'TEXT', 'to': to, 'body': text, } - if sendTochannel == 1 : + if sendTochannel == True : post_data['majorType']="CHANNEL" if keyboard is not None: post_data['keyboard'] = keyboard return self.send_message(post_data) - def send_file(self, to, body, file_name, file_type, file_url, file_size, extra_params={}): + def send_file(self, to, body, file_name, file_type, file_url, file_size, extra_params={},sendTochannel=False): + '''if you set sendTochannel =True ,it will send message to channel + (to=id of channel without @ )''' post_data = { 'to': to, 'body': body, @@ -116,22 +118,26 @@ def send_file(self, to, body, file_name, file_type, file_url, file_size, extra_p 'fileUrl': file_url, 'fileSize': file_size } - + if sendTochannel == True : + post_data['majorType']="CHANNEL" for key, value in extra_params.items(): post_data[key] = value return self.send_message(post_data) def send_image(self, to, image_file_url, image_file_name, image_file_size, image_width=0, - image_height=0, thumbnail_file_url=None, caption='', keyboard=None): + image_height=0, thumbnail_file_url=None, caption='',sendTochannel=False, keyboard=None): + '''if you set sendTochannel =True ,it will send message to channel + (to=id of channel without @ )''' image_file_type = 'IMAGE' extra_params = { 'imageWidth': 0, 'imageHeight': 0, 'thumbnailUrl': '' } - + if sendTochannel == True : + extra_params['majorType']="CHANNEL" if int(image_width) and int(image_height): extra_params['imageWidth'] = int(image_width) extra_params['imageHeight'] = int(image_height) @@ -144,15 +150,18 @@ def send_image(self, to, image_file_url, image_file_name, image_file_size, image extra_params) def send_gif(self, to, image_file_url, image_file_name, image_file_size, image_width=0, - image_height=0, thumbnail_file_url=None, caption='', keyboard=None): + image_height=0, thumbnail_file_url=None, caption='',sendTochannel=False, keyboard=None): + '''if you set sendTochannel =True ,it will send message to channel + (to=id of channel without @ )''' gif_file_type = 'GIF' extra_params = { 'imageWidth': 0, 'imageHeight': 0, 'thumbnailUrl': '' } - + if sendTochannel == True : + extra_params['majorType']="CHANNEL" if int(image_width) and int(image_height): extra_params['imageWidth'] = int(image_width) extra_params['imageHeight'] = int(image_height) @@ -165,8 +174,10 @@ def send_gif(self, to, image_file_url, image_file_name, image_file_size, image_w extra_params) def send_video(self, to, video_file_url, video_file_name, video_file_size, video_duration_in_milliseconds, - video_width=0, video_height=0, thumbnail_file_url=None, caption='', keyboard=None): + video_width=0, video_height=0, thumbnail_file_url=None, caption='',sendTochannel=False, keyboard=None): + '''if you set sendTochannel =True ,it will send message to channel + (to=id of channel without @ )''' video_file_type = 'VIDEO' extra_params = { 'thumbnailWidth': 0, @@ -175,6 +186,8 @@ def send_video(self, to, video_file_url, video_file_name, video_file_size, video 'fileDuration': video_duration_in_milliseconds } + if sendTochannel == True : + extra_params['majorType']="CHANNEL" if int(video_width) and int(video_height): extra_params['imageWidth'] = int(video_width) extra_params['imageHeight'] = int(video_height) @@ -187,21 +200,26 @@ def send_video(self, to, video_file_url, video_file_name, video_file_size, video extra_params) def send_voice(self, to, voice_file_url, voice_file_name, voice_file_size, voice_duration_in_milliseconds, - caption='', keyboard=None): + caption='',sendTochannel=False, keyboard=None): + '''if you set sendTochannel =True ,it will send message to channel + (to=id of channel without @ )''' voice_file_type = 'PUSH_TO_TALK' extra_params = { 'fileDuration': voice_duration_in_milliseconds } + if sendTochannel == True : + extra_params['majorType']="CHANNEL" if keyboard is not None: extra_params['keyboard'] = keyboard return self.send_file(to, caption, voice_file_name, voice_file_type, voice_file_url, voice_file_size, extra_params) - def send_location(self, to, latitude, longitude, caption='', keyboard=None): - + def send_location(self, to, latitude, longitude, caption='',sendTochannel=False, keyboard=None): + '''if you set sendTochannel =True ,it will send message to channel + (to=id of channel without @ )''' post_data = { 'type': 'LOCATION', 'latitude': latitude, @@ -209,17 +227,22 @@ def send_location(self, to, latitude, longitude, caption='', keyboard=None): 'to': to, 'body': caption } - + + if sendTochannel == True : + post_data['majorType']="CHANNEL" if keyboard is not None: post_data['keyboard'] = keyboard return self.send_message(post_data) - def send_attachment(self, to, file_url, file_name, file_size, caption='', keyboard=None): - + def send_attachment(self, to, file_url, file_name, file_size, caption='',sendTochannel=False, keyboard=None): + '''if you set sendTochannel =True ,it will send message to channel + (to=id of channel without @ )''' file_type = 'ATTACHMENT' extra_params = {} + if sendTochannel == True : + extra_params['majorType']="CHANNEL" if keyboard is not None: extra_params['keyboard'] = keyboard @@ -328,15 +351,16 @@ def download_file(self, file_url, save_file_path): def upload_file(self, file_path): if not os.path.isfile(file_path): - raise ValueError('Invalid file (file with this name not exit or maybe remove or incorrect path you given)') - try: - session = requests.Session() - with open(file_path, "rb") as f: - m = MultipartEncoder({ - 'file':(file_path.split('/')[-1],f,"application/octet-stream") - }) - headers = {"Content-Type": m.content_type} - response = session.post(self.get_upload_file_url(),headers=headers, data=m) + raise ValueError('Invalid file (file with this name not exit or maybe remove or incorrect path you given)') + + try: + session = requests.Session() + with open(file_path, "rb") as f: + m = MultipartEncoder({ + 'file':(file_path.split('/')[-1],f,"application/octet-stream") + }) + headers = {"Content-Type": m.content_type} + response = session.post(self.get_upload_file_url(),headers=headers, data=m) session.close() if response.status_code == 200: From e50f2cb4a0cc62ca49fac7fa3b03423f6300a16f Mon Sep 17 00:00:00 2001 From: Mahdiali313 <52355596+Mahdiali313@users.noreply.github.com> Date: Wed, 12 May 2021 15:09:11 +0430 Subject: [PATCH 22/23] Update SoroushBot.py --- SoroushBot.py | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/SoroushBot.py b/SoroushBot.py index ac12146..ba694ec 100644 --- a/SoroushBot.py +++ b/SoroushBot.py @@ -10,8 +10,8 @@ def __init__(self, bot_token): global b b= Client(bot_token) - def sendText(self,target_id,caption): - resualt=b.send_text(target_id,caption) + def sendText(self,target_id,caption,isSendToChannel=False): + resualt=b.send_text(target_id,caption,isSendToChannel) #resualt=[error,success] return resualt @@ -21,7 +21,7 @@ def imageVideoResalotion(self,path): height = int(round(clip.h)) return [width,height] - def sendImage(self,target_id,image_path,image_thumbnail_path,caption): + def sendImage(self,target_id,image_path,image_thumbnail_path,caption,isSendToChannel=False): rImage=b.upload_file(image_path) #rImage=[image_error, image_url] @@ -34,7 +34,7 @@ def sendImage(self,target_id,image_path,image_thumbnail_path,caption): ntpath.basename(image_path), getsize(image_path), scale[0], scale[1], rThumb[1], - caption) + caption,isSendToChannel) #if(resualt[1] != 'OK'): # print("paramets that gotten this function",image_path,image_thumbnail_path,caption) return resualt @@ -45,7 +45,7 @@ def sendImage(self,target_id,image_path,image_thumbnail_path,caption): return rImage - def sendVideo(self,target_id,video_path,video_thumbnail_path,caption,durationSeconds=None): + def sendVideo(self,target_id,video_path,video_thumbnail_path,caption,durationSeconds=None,isSendToChannel=False): rVideo=b.upload_file(video_path) #rVideo=[video_error, video_url] @@ -64,7 +64,7 @@ def sendVideo(self,target_id,video_path,video_thumbnail_path,caption,durationSec getsize(video_path), durationSeconds*1000, scale[0], scale[1], rThumb[1], - caption) + caption,isSendToChannel) #if(resualt[1] != 'OK'): # print("paramets that gotten this func",video_path,video_thumbnail_path,caption,durationSeconds) return resualt @@ -75,21 +75,21 @@ def sendVideo(self,target_id,video_path,video_thumbnail_path,caption,durationSec return rVideo - def sendFile(self,target_id,file_path,caption): + def sendFile(self,target_id,file_path,caption,isSendToChannel=False): rFile = b.upload_file(file_path) #rFile=[error, file_url] resualt = b.send_attachment(target_id, rFile[1], ntpath.basename(file_path), getsize(file_path), - caption) + caption,isSendToChannel) return resualt - def sendLocation(self,target_id, latitude, longitude, caption='', keyboard=None): - resualt=b.send_location(target_id, latitude, longitude, caption, keyboard) + def sendLocation(self,target_id, latitude, longitude, caption='', keyboard=None,isSendToChannel=False): + resualt=b.send_location(target_id, latitude, longitude, caption, keyboard,isSendToChannel) return resualt - def sendAudio (self, target_id, audio_path, caption, audio_duration): + def sendAudio (self, target_id, audio_path, caption, audio_duration,isSendToChannel=False): audioUrl=b.upload_file(audio_path) audiotype = 'PUSH_TO_TALK' @@ -100,6 +100,6 @@ def sendAudio (self, target_id, audio_path, caption, audio_duration): resualt = b.send_file(target_id, caption, ntpath.basename(audio_path), audiotype, audioUrl[1], getsize(audio_path), - extra_params) + extra_params,isSendToChannel) return resualt From 0baa005b7f8a89dae182fcb39279cf45b9bd51a8 Mon Sep 17 00:00:00 2001 From: Mahdiali313 <52355596+Mahdiali313@users.noreply.github.com> Date: Wed, 12 May 2021 15:18:20 +0430 Subject: [PATCH 23/23] Update README.md --- README.md | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 996964f..2c4512e 100644 --- a/README.md +++ b/README.md @@ -27,9 +27,15 @@ to = 'user chat_id' [error, success] = bot.sendText(to, 'Your text') if success: - print('Message sent successfully') + print('Message sent successfully to user') +channelid = 'channel id without @' + +[error, success] = bot.sendText(channelid, 'Your text',isSendToChannel=True) + +if success: + print('Message sent successfully to channel') ``` "to" value in above example is chat_id of a bot user. You can find it in front of 'from' key in a message that user has sent to your bot.