Connection pool of Request object is smaller than optimal value (8) - bots

thank you for being here, while coding a telegram bot I've ran into this issue:
Connection pool of Request object is smaller than optimal value (8)
Could you help? Here's the code (I've removed my bot token)
I've tried adding Create a Session object with a larger connection pool size but to no avail, it keeps refusing, what any other ideas do you guys have?
I've checked the required libraries.
import os
import requests
import telegram
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters
# Replace YOUR_TOKEN_HERE with your actual bot token
bot = telegram.Bot(token='')
# Set up the Updater and Dispatcher
updater = Updater(bot=bot, use_context=True)
dispatcher = updater.dispatcher
# Set up a global variable to store the watermark text
watermark_text = None
# Set up a global variable to store the watermark text opacity (default to 50)
watermark_opacity = 50
def start(update, context):
"""Handler for the /start command"""
context.bot.send_message(chat_id=update.effective_chat.id, text="Hi! I'm a bot that can add watermark text to videos. Use the /setwatermark command to set the watermark text, and the /opacity command to set the text opacity. Use the /watermark command to watermark a video.")
def set_watermark(update, context):
"""Handler for the /setwatermark command"""
# Declare the watermark_text variable as global
global watermark_text
# Get the watermark text from the user's message
watermark_text = " ".join(context.args)
# Save the watermark text to the global variable
watermark_text = watermark_text
context.bot.send_message(chat_id=update.effective_chat.id, text="Watermark text set! You can now use the /watermark command to watermark a video.")
def set_opacity(update, context):
"""Handler for the /opacity command"""
# Declare the watermark_opacity variable as global
global watermark_opacity
# Get the watermark text opacity from the user's message
watermark_opacity = int(context.args[0])
def watermark(update, context):
"""Handler for the /watermark command"""
# Check if a watermark text has been set
if watermark_text is None:
context.bot.send_message(chat_id=update.effective_chat.id, text="No watermark text has been set. Use the /setwatermark command to set the watermark text.")
return
# Create a Session object with a larger connection pool size
session = requests.Session()
adapter = requests.adapters.HTTPAdapter(max_pool_size=8)
session.mount('https://', adapter)
# Get the file ID of the video sent by the user
file_id = update.message.video.file_id
# Download the video file using the Session object
file = session.get(bot.get_file(file_id).file_path)
open('input.mp4', 'wb').write(file.content)
# Watermark the video using ffmpeg
os.system(f'ffmpeg -i input.mp4 -vf "drawtext=fontfile=arial.ttf: text={watermark_text}: fontcolor=white: fontsize=48: x=10: y=10: alpha={watermark_opacity}" output.mp4')
# Send the watermarked video back to the user
context.bot.send_video(chat_id=update.effective_chat.id, video=open('output.mp4', 'rb'))
# Set up the command handlers
start_handler = CommandHandler('start', start)
dispatcher.add_handler(start_handler)
set_watermark_handler = CommandHandler('setwatermark', set_watermark)
dispatcher.add_handler(set_watermark_handler)
set_opacity_handler = CommandHandler('opacity', set_opacity)
dispatcher.add_handler(set_opacity_handler)
watermark_handler = CommandHandler('watermark', watermark)
dispatcher.add_handler(watermark_handler)
# Start the bot
updater.start_polling()

I've had the same but I found a way. You can set the connection pool manually to 8 like this:
from telegram.utils.request import Request
request = Request(con_pool_size=8)
bot = telegram.Bot(token="", request=request)

Related

How to make share path monitoring script to generate outlook email alert if no movement appears in shard path after 20 minutes?

Greetings!! I am trying using below watchdog module to monitor a shared path that works fine but I am not getting the idea to
generate outlook email alert, after the time span of 20 minutes if no
modification or update happening to the specified path. Below is the
code:
import os
import sys
import time
import logging
from watchdog.observers import Observer
from watchdog.events import LoggingEventHandler
if __name__ == "__main__":
logging.basicConfig(level=logging.INFO,
format='%(asctime)s - %(message)s',
datefmt='%Y-%m-%d %H:%M:%S')
os.system('//fleet.ad/data/Data4/VMSSHARE/its/DOCS')
print("found")
# Defining your own path
path = "//bleet.ad/data/Data4/VMSSHARE/its/DOCS"
print("found")
# Initilaize logging event handler
event_handler = LoggingEventHandler()
# Initialize Observer
observer = Observer()
observer.schedule(event_handler, path, recursive=True)
# Start the observer
observer.start()
try:
while True:
# set the thread sleep time
time.sleep(1)
except KeyboardInterrupt:
observer.stop()
observer.join()
However, tried this piece of code to receive outlook email alerts, but not sure how to make it work with above script:
import os
import smtplib
import requests
EMAIL_ADDRESS = os.environ.get('USER_ID')
EMAIL_PASSWORD = os.environ.get('USER_PASSWORD')
r = requests.get("https://fleet.my.salesforce.com", timeout=5)
#if r.status_code!= 200:
with smtplib.SMTP('smtp.office365.com', 587) as smtp:
smtp.ehlo()
smtp.starttls()
smtp.ehlo()
smtp.login(EMAIL_ADDRESS, EMAIL_PASSWORD)
subject = 'ALARM: MAPILab is stuck from copying Public folders to destination'
body = 'Make sure server is restarted and it is backed up'
msg = f'Subject:{subject}\n\n{body}'
smtp.sendmail(EMAIL_ADDRESS, 'ryadav#elementcorp.com', msg)
Challenge for me is :
r = requests.get("https://fleet.my.salesforce.com", timeout=5)
Instead of website monitor , how should I ask to look for the code output?
You have to attach methods to your event handler, like this:
my_event_handler.on_created = on_created
my_event_handler.on_deleted = on_deleted
my_event_handler.on_modified = on_modified
my_event_handler.on_moved = on_moved
where the methods look like:
def on_created(event):
print(f"{event.src_path} has been created!")
as described in this blog post: http://thepythoncorner.com/dev/how-to-create-a-watchdog-in-python-to-look-for-filesystem-changes/
Have your handlers update a last_changed timestamp variable whenever there is a change. (Since all your handlers do the same thing regardless of what the change was, you could get away with just defining one handler method, maybe call it on_any_change and attach it to all 4 handler methods. Oh look, the watchdog docs show that there is an on_any_event method, so you could just hook into that.)
Then in your while True loop, if the current time - last_changed is greater than your threshold, send the email.

Plot a continuous graph of Number of Snort alerts against time

I have snort logging DDOS alerts to file; I use Syslog-ng to parse the logs and output in json format into redis (wanted to set it up as a buffer, I use 'setex' command with expiry of 70 secs).
The whole thing seems not to be working well; any ideas to make it easier is welcome.
I wrote a simple python script to listen to redis KA events and count the number of snort alerts per second. I tried creating two other threads; one to retrieve the json-formatted alerts from snort and the second to count the alerts. The third is supposed to plot a graph using matplotlib.pyplot
#import time
from redis import StrictRedis as sr
import os
import json
import matplotlib.pyplot as plt
import threading as th
import time
redis = sr(host='localhost', port = 6379, decode_responses = True)
#file = open('/home/lucidvis/vis_app_py/log.json','w+')
# This function is still being worked on
def do_plot():
print('do_plot loop running')
while accumulated_data:
x_values = [int(x['time_count']) for x in accumulated_data]
y_values = [y['date'] for y in accumulated_data]
plt.title('Attacks Alerts per time period')
plt.xlabel('Time', fontsize=14)
plt.ylabel('Snort Alerts/sec')
plt.tick_params(axis='both', labelsize=14)
plt.plot(y_values,x_values, linewidth=5)
plt.show()
time.sleep(0.01)
def accumulator():
# first of, check the current json data and see if its 'sec' value is same
#that is the last in the accumulated data list
#if it is the same, increase time_count by one else pop that value
pointer_data = {}
print('accumulator loop running')
while True:
# pointer data is the current sec of json data used for comparison
#new_data is the latest json formatted alert received
# received_from_redis is a list declared in the main function
if received_from_redis:
new_data = received_from_redis.pop(0)
if not pointer_data:
pointer_data = new_data.copy()
print(">>", type(pointer_data), " >> ", pointer_data)
if pointer_data and pointer_data['sec']==new_data["sec"]
pointer_data['time_count'] +=1
elif pointer_data:
accumulated_data.append(pointer_data)
pointer_data = new_data.copy()
pointer_data.setdefault('time_count',1)
else:
time.sleep(0.01)
# main function creates the redis object and receives messages based on events
#this function calls two other functions and creates threads so they appear to run concurrently
def main():
p = redis.pubsub()
#
p.psubscribe('__keyspace#0__*')
print('Starting message loop')
while True:
try:
time.sleep(2)
message = p.get_message()
# Obtain the key from the redis emmitted event if the event is a set event
if message and message['data']=='set':
# the format emmited by redis is in a dict form
# the key is the value to the key 'channel'
# The key is in '__keyspace#0__*' form
# obtain the last field of the list returned by split function
key = message['channel'].split('__:')[-1]
data_redis = json.loads(redis.get(str(key)))
received_from_redis.append(data_redis)
except Exception e:
print(e)
continue
if __name__ == "__main__":
accumulated_data = []
received_from_redis = []
# main function creates the redis object and receives messages based on events
#this function calls two other functions and creates threads so they appear to run concurrently
thread_accumulator = th.Thread(target = accumulator, name ='accumulator')
do_plot_thread = th.Thread(target = do_plot, name ='do_plot')
while True:
thread_accumulator.start()
do_plot_thread.start()
main()
thread_accumulator.join()
do_plot_thread.join()
I currently do get errors per se ; I just cant tell if the threads are created or are working well. I need ideas to make things work better.
sample of the alert formated in json and obtained from redis below
{"victim_port":"","victim":"192.168.204.130","protocol":"ICMP","msg":"Ping_Flood_Attack_Detected","key":"1000","date":"06/01-09:26:13","attacker_port":"","attacker":"192.168.30.129","sec":"13"}
I'm not sure I understand exactly your scenario, but if you want to count events that are essentially log messages, you can probably do that within syslog-ng. Either as a Python destination (since you are already working in python), or maybe even without additional programming using the grouping-by parser.

Handling VBA popup message boxes in Microsoft Excel

I'm required to automate an Excel file with Python, the problem is that the Excel has macro in it , when clicking on a macro button it presents a popup:
The popup message box itself has a button that I must click to proceed.
I've tried with both Win32com and Win32 API libraries in Python but I was unable to find any solution in the documentation.
my code is as follows :
ms_excel_app = win32.gencache.EnsureDispatch('Excel.Application')
ms_excel_app.Visible = True
ms_excel_app.DisplayAlerts = False
template_file = r"C:\Templates_for_test\macro_test.xlsm"
ms_excel_file =ms_excel_app.Workbooks.Open(template_file)
ms_excel_file.DisplayAlerts = False
excel_sheet = ms_excel_file.ActiveSheet
# Opening a template file
ms_excel_file = ms_excel_app.Workbooks.Open(template_file)
spread_sheet = ms_excel_file.ActiveSheet
# Click on 'Current date and time' macro button
ms_excel_app.Application.Run("Sheet1.CommandButton1_Click")
# TODO verify verify timestamp and click ok- popup appears verify date and time format : dd/mm/yyyy hh:mm:ss
timestamp_message = time.strftime("%d/%m/%Y %H:%M:%S")
what I'm trying to do is to identify the popup in the screenshot and click on the 'OK' button
A bit late, just post a solution since I faced the same case.
The message box gets Excel application stuck and accordingly blocks your main process. So, a general solution:
start a child thread to listen to the message box -> close it once found
do your main work with the Excel file
stop the child thread when everything is finished
As you're using pywin32, it can also be used to catch and close message box. A Thread class to do the job:
# ButtonClicker.py
import time
from threading import Thread, Event
import win32gui, win32con
class ButtonClicker(Thread):
def __init__(self, title:str, interval:int):
Thread.__init__(self)
self._title = title
self._interval = interval
self._stop_event = Event()
def stop(self):
'''Stop thread.'''
self._stop_event.set()
#property
def stopped(self):
return self._stop_event.is_set()
def run(self):
while not self.stopped:
try:
time.sleep(self._interval)
self._close_msgbox()
except Exception as e:
print(e, flush=True)
def _close_msgbox(self):
# find the top window by title
hwnd = win32gui.FindWindow(None, self._title)
if not hwnd: return
# find child button
h_btn = win32gui.FindWindowEx(hwnd, None,'Button', None)
if not h_btn: return
# show text
text = win32gui.GetWindowText(h_btn)
print(text)
# click button
win32gui.PostMessage(h_btn, win32con.WM_LBUTTONDOWN, None, None)
time.sleep(0.2)
win32gui.PostMessage(h_btn, win32con.WM_LBUTTONUP, None, None)
time.sleep(0.2)
Finally, your code may look like:
from ButtonClicker import ButtonClicker
# 1. start a child thread
# ButtonClicker instance tries to catch window with specified `title` at a user defined frequency.
# In your case, the default title for Excel message box is `Microsoft Excel`.
listener = ButtonClicker("Microsoft Excel", 3)
listener.start()
# 2. do your work with Excel file as before
# though it might be blocked by message box, the concurrent child thread will close it
ms_excel_app = win32.gencache.EnsureDispatch('Excel.Application')
ms_excel_app.Visible = True
ms_excel_app.DisplayAlerts = False
template_file = r"C:\Templates_for_test\macro_test.xlsm"
...
# 3. close the child thread finally
listener.stop()

how to send commands to web terminal?

here is my code
It only creates terminal on web using 'bash' command.
I want to add functionality to this code similar to the interactive labs work. Which has terminal on one side and content part on other side. I want to run command on mouseclick function to my web terminal, and I am searching for solution how to do that can you suggest something?
class WebmuxTermManager(terminado.SingleTermManager):
def get_terminal(self, port_number):
self.shell_command = ["bash"]
term = self.new_terminal()
self.start_reading(term)
return term
class TerminalPageHandler(tornado.web.RequestHandler):
def get_host(self, port_number):
pass
def get(self, port_number):
return self.render("term.html", static=self.static_url, ws_url_path="/_websocket/"+port_number, hostname=self.get_host(port_number))
if __name__ == "__main__":
term_manager = WebmuxTermManager(shell_command=('bash'))
handlers = [
(r"/_websocket/(\w+)", terminado.TermSocket, {'term_manager': term_manager}),
(r"/shell/([\d]+)/?", TerminalPageHandler),
(r"/webmux_static/(.*)", tornado.web.StaticFileHandler, {'path':os.path.join(TEMPLATE_DIR,"webmux_static")}),
]
application = tornado.web.Application(handlers, static_path=STATIC_DIR,template_path=TEMPLATE_DIR,term_manager=term_manager,debug=True)
application.listen(8888)
try:
IOLoop.current().start()
except KeyboardInterrupt:
logging.info("\nShuttiing down")
finally:
term_manager.shutdown()
IOLoop.current().close()
First I dont know what terminado is, so I will stick to tornado's websockets,
Make a websocket class to send and recieve messages as per your ruleset
class WebsocketHandler(tornado.websocket.WebSocketHandler):
def check_origin(self, origin):
# deals with allowing certain ip address ranges or domain_names to
# connect to your websocket
pass
def open(self):
# perform some logic when the socket is connected to the client side
# eg give it a unique id to and append it to a list etc, its up to you
pass
def on_message(self, command):
# this is where your problem lies
# act on the message
send_back = self.runCommand(command)
self.write_message(send_back)
def on_close(self):
# delete the socket
pass
def runCommand(self, command):
# import shlex
# import supbrocess
cmd = shlex.split(command)
process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout, stderr = process.communicate()
return dict(
stdout=stdout.decode(),
stderr=stdout.decode()
)
The route for WebsocketHandler class is
(r"/websocket_test", WebsocketHandler)
Hook it up to your routes and start the tornado server
On the client side
Connect using javascript as follows:
//secure or unsecure, up to you.
unsecure_test_conn = new WebSocket('ws://ip_address_or_domain_name/websocket_test')
secure_test_conn = new WebSocket('wss://ip_address_or_domain_name/websocket_test')
unsecure_test_conn.onmessage = function(event){
data = JSON.parse(event.data)
//act on the data as you see fit and probably send it back to server
send_back = parseMessage(data)
unsecure_test_conn.send(send_back)
}
This should get you started on how to send information back and forth on the web.

Python while True preventing previous code execution

I'm building a telegram bot using telepot(python) and a weird thing happened:
since the bot is supposed to keep running, I've set a
while 1:
time.sleep(.3)
at the end of my bot, right after I define how to handle the MessageLoop.
The bot has some print() to assert that the bot is setting up (or better, that it's setting up the message handler) and that it is reading the chat, waiting for any input.
The problem is that if I run the code without the
while 1:
time.sleep(.3)
it prints the messages and stops the execution (not having a loop to wait for new input), but if I add the while 1: (...) the code stops before being able to print anything.
Here's the code:
"""Launch the bot."""
import json
import telepot
from telepot.loop import MessageLoop
import time
from base import splitter
from base import handler
messageHandler = handler.Handler()
with open('triggers.json') as f:
triggers = json.load(f)
with open('config.json') as f:
config = json.load(f)
botKey = config['BOT_KEY']
# define the bot and the botname
bot = telepot.Bot(botKey)
botName = bot.getMe()['username']
# split commands in arrays ordered by priority
configSplitter = splitter.Splitter()
triggers = configSplitter.splitByPriority(triggers)
# define the handler
messageHandler.setBotname(botName)
messageHandler.setMessages(triggers)
# handle the messageLoop
print("setting up the bot...")
MessageLoop(bot, messageHandler.handle).run_as_thread()
print("Multibot is listening!")
# loop to catch all the messages
while 1:
time.sleep(.3)
Python version: 3.6 32-bit
The solution was to add sys.stdout.flush() after the various print()to restore the functionality of print(), while in order to make the code work again i had to change the telepot function
MessageLoop(bot, messageHandler.handle).run_as_thread()
which wasn't working properly to:
bot.message_loop(messageHandler.handle)

Resources