Telegram: Send message to bot from different script - python

I have a telegram bot that handles user input to perform actions on links that are sent.
In my Telegram Bot I have:
def handle_message(bot, update):
url = update.message.text
# do parsing and add url to database
dispatcher.add_handler(MessageHandler(filters=Filters.text, callback=handle_message))
However I want to also be able to send a URL to the bot from a POST request. i.e. use the telegram bot's API to send a message to itself so it can parse the link.
How can I send a POST request to my telegram bot and have it run handle_message() on the input?

It is technically impossible for bots to communicate with each other (and therefore with themselves).

Related

Why can't I send messages to Telegram bot?

I'm using Telebot to build a simple Telegram bot. I've set up a message handler that responds to commands succesfully, but when I try to send a single message I get an error if I use the chat id (ex: 1234567890):
Error code: 403. Description: Forbidden: bot can't send messages to
bots
I get a different error when I use the user id (ex: #my_user):
Error code: 400. Description: Bad Request: chat not found
This is my code, the auth is correct:
tg_bot = telebot.TeleBot(TG_TOKEN, parse_mode='MARKDOWN')
tg_bot.send_message(chat_id=CHAT_ID_USER, text="hola test")
Is the bot's chat different to the chat I'm supposed to talk with? Any solution and details about the bot functionality will be apreciated, I'm still learning about that!
try this
import telebot
bot = telebot.TeleBot(token)
#bot.message_handler(commands=['start'])
def start(message):
bot.send_message("1395609507","Hello")
bot.infinity_polling()
If the user under this id has not started your bot, then the bot will not be able to write to him/her first. And in this case it is natural to get the error "chat not found". This error can also be observed when working with users who first used the bot and then blocked it.

Using Python to send messages in Discord

Is it possible to make a Python program that sends messages in a Discord chat every 2 minutes, but without other users being able to see that it is a program?
You can make a bot to send message every two minutes using the below code
from discord.ext import commands,tasks
#tasks.loop(seconds=120)
async def determine_winner():
message=#Message to be sent
channel_id=#Gives the id of channel where to send
client.get_channel(channel_id).send(message)
If you want to send this message in multiple channel. You should get the id of each channel where the message to be sent and store it in database or json file and fetch it at the time of sending the message.
But self-botting is against discord'sTOS if they found you self-botting they will ban you from discord permanently

How to get an old message ID with python telegram bot?

I have a Telegram channel with a bot inside it, which I use to send messages to the channel and edit them with this Python code
import telegram
bot = telegram.Bot(token=MY_TOKEN)
msg = bot.send_message(chat_id=MY_CHANNEL_ID, text="...") # send a message
msg = msg.edit_text("...") # edit the previous message
Sometimes the python bot crashes/closes for whatever reason, when I start the bot again how to recover the ID of a message sent during the previous session so that I'd be able to edit the message again?
I read about this method
bot.edit_message_text(text, chat_id=update.callback_query.message.chat_id,
message_id=update.callback_query.message.message_id)
I guess that for chat_id I have to use MY_CHANNEL_ID but what I don't understand is how to get the message_id of the old message
MANUAL SOLUTION
I found out that the message_id is just the number contained in the post link, so a workaround is to manually copy the post link of a message from the telegram client, for example if the post link is https://t.me/my_channel_name/207 then the message_id is 207 and to edit that message we have to run
bot.edit_message_text(text="...", chat_id=MY_CHANNEL_ID, message_id=207)

Bot which interacts with telegram bots

I was wondering if I can automatically download files from other telegram bots. I've searched online how to make a Python bot (or a Telegram bot written in Python) which do this, but I didn't find anything. Can someone help me?
Interaction between bots in telegram directly isn't possible since the Bot API doesn't support that.
But you can use MTProto libraries to automate almost all interactions with bots (including file downloading). since these libs simply automate regular user accounts, they don't have the limitations of the bot api.
Here is an example to download a file using telethon lib :
from telethon import TelegramClient, events
api_id = <API_ID>
api_hash = '<API_HASH>'
client = TelegramClient('session', api_id, api_hash)
BOT_USER_NAME="#filesending_sample_bot" # the username of the bot that sends files (images, docs, ...)
#client.on(events.NewMessage(func=lambda e: e.is_private))
async def message_handler(event):
if event.message.media is not None: # if there's something to download (media)
await client.download_media(message=event.message, )
async def main():
await client.send_message(BOT_USER_NAME, 'some text or command to trigger file sending') # trigger the bot here by sending something so that the bot sends the media
client.start()
client.loop.run_until_complete(main())
client.run_until_disconnected()
and in my example I created a minimal telegram bot in javascript that sends a photo (as Document) as it receives any message to test the above script out (but you configure the above script to match with your case):
const bot = new (require("telegraf"))(<MY_BOT_TOKEN>);
bot.on("message", (ctx) => ctx.replyWithDocument("https://picsum.photos/200/300"));
bot.launch();
Note that you must connect using a regular telegram account (not using a bot token but rather a phone number) for this to work. If using a telegram bot is a must, you can use the script in the background (by running it as a new process or as a REST api, etc...) and return a result to the telegram bot.

send direct message to slack user from a app bot but not in app channel

Is there a way to send direct message to from a bot(myapp/user) to a user say xyz or to a user's slackbot. The message needs to appear against the user and not on the mybot app.
I am using the python slack-client.
Using below code to send the message:
user_id="<touser>"
im_channel=self.open_dm(user_id)
slack_client.api_call("chat.postMessage",channel=im_channel,text="hi buddy", as_user=True)
The above code posts the message in the myapp app channel. Is there a way for the bot to send the message directly to the user and not in the app channel?
OR
Is there a way for the myapp bot to send to slackbot channel addressing the user?
Yes.
Just send a message with the user ID for channel and it will appear in the slackbot channel of that user.
Something like this:
user_id="<touser>"
slack_client.api_call("chat.postMessage",channel=user_id,text="hi buddy")
However, note that every message on Slack must to use a channel that includes so called "direct messages". That is how Slack works.

Categories

Resources