How to send message using python-telegram-bot - python

I'm new using python and telegram-bot
I'm trying to found a way to send messages using python-telegram-bot when I received a message without uses:
requests.post(
'https://api.telegram.org/bot<token>/sendMessage?chat_id=<id>&text=Hello World!')
Flow:
Message arrived on channel A then send a new message to channel B (Message receveid on Channel A).
I only found reply_text. Is there other way to do it ?
Thanks !

reply_text won't work because it will send the message back to channel A. You would need bot.send_message where you can set the target channel with chat_id. A simple example:
def update_message_to_channel_B(update: object, context: CallbackContext) -> None:
context.bot.send_message(chat_id=CHANNEL_B_ID, text='Message receveid on Channel A')
And add the handler:
dispatcher.add_handler(MessageHandler(Filters.text, update_message_to_channel_B))

Related

Get message ID of last sent message from Discord.py webhook

This is the sync code for my discord embed, I've been trying to get the message ID of the message sent so that I can edit it, I'm trying to automate some tasks but been unsuccessful so far.
Here's the code:
embed = discord.Embed(title="Post Title")
embed.add_field(name="Req AMount", value="100")
embed.add_field(name="Return AMount", value="120")
webhook = SyncWebhook.partial(1055498127618097174, "prxoD_ZW0vO6ghrwZNhOiwI9AeLBfNYYss6MTLPkjgvAb_B3WJgWNLMMtPWYZ67GTYbn")
sending = webhook.send(embed=embed)
Any help would be appreciated thanks
In the webhook.send message, if you set the parameter wait to True, you will get a WebhookMessage object returned, which you can then use the edit/delete the message your webhook sent.
Note that the WebhookMessage object doesn't have the ID parameter but has an edit and delete methods. Typically, as webhooks are used to send messages to channels/servers you might not have a bot in/permissions for, what you can do with the returned message object is a little less.
Webhook docs send here
WebhookMessage docs here

Replying with a image or gif obtained via API, in telegram bot python (telegram.ext)

I am building a meme generator telegram bot that sends a meme whenever the user commands /meme. I am using an API, say "https://......meme-api.....". It gives me a URL for the meme. I want to send the media in that URL provided by the API, as a reply to the /meme command.
dispatcher.add_handler(telegram.ext.CommandHandler("meme", meme))
def meme(update, context):
response = requests.get('https://meme-api.herokuapp.com/gimme').json()
url = response.get('url')
print(url)
update.message.reply_text(url)
I can't wrap my head around how I can write the meme function. What message reply type should I use to send the media without saving it?
As explained in the documentation of Message.reply_text that method is a shortcut for Bot.send_message, which corresponds to sendMessage in the Telegram docs. This method sends text messages.
To send photos & animantions, you'll have to use
Message.reply_photo / Bot.send_photo / sendPhoto
Message.reply_animantion / Bot.send_animantion / sendAnimantion,
respectively.

How do I get a slack bot that is sending a message via Python to post #here and ping the channel?

Currently my slack app / bot will post a message being sent via Python that has #here or #channel in the text but the #here or #channel won't be highlighted or actually ping the channel. Here is the code:
import slack
def slack_notif(slackchannel,message):
client = slack.WebClient(token='xxxmytokenxxx')
client.chat_postMessage(channel=slackchannel, text=message)
slack_notif(
slackchannel = 'mychannel',
message = '<#here> This is a test'
)
Output = #here This is a test
Intended output:
To use #here & #channel as 'mentions',
they are marked as :
<!here> & <!channel>

How to add a Slack reaction to a send_card

I have an Errbot function that sends a Slack card. How do I then add a reaction to the card instead of the original message(msg) that was received?
#botcmd
def example(self, msg):
self.send_card(title='Test',
body='test123',
thumbnail=' ',
image=' ',
link=' ',
color='green',
in_reply_to=msg)
self._bot.add_reaction(card_msg??, 'grey_question')
send_card does not return to you the message it sends which means you will have to do something to get the message info of the card you sent.
One option would be to trigger a callback for all messages in your plugin, inspect the message, and add your reaction there:
Another option could be to use the slack backend api call method to search for your message and add the reaction that way.

Telegram Bot "chat not found"

I have the following code in Python to send a message to myself from a bot.
import requests
token = '123456789:ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHI'
method = 'sendMessage'
myuserid = 1949275XX
response = requests.post(
url='https://api.telegram.org/bot{0}/{1}'.format(token, method),
data={'chat_id': myuserid, 'text': 'hello friend'}
).json()
print(response)
but this returns {'description': 'Bad Request: chat not found', 'error_code': 400, 'ok': False}
What am I doing wrong? I got myuserid by sending /getid to #myidbot and I got my token from #BotFather
As #maak pointed out, you need to first send a message to the bot before the bot can send messages to you.
I was using prefix # before the value of chat_id as suggested everywhere. I removed it and it started working.
Note: if your chat id is 12345678 then you need to prefix it with -100 such that it is -10012345678.
Example Postman call:
/sendMessage?chat_id=-10012345678&text=Let's get together
If your trying to send messages to a group, you must add a ‘-‘ in front of your chat ID.
For example:
TELEGRAM_REG_CHAT_ID="1949275XX"
should be
TELEGRAM_REG_CHAT_ID="-1949275XX"
There is a way to send notifications messages to telegram. It's a bit tricky but the tutorial is great!
http://bernaerts.dyndns.org/linux/75-debian/351-debian-send-telegram-notification
I just sended a message of my apache state to a privat channel.
Works also on public channel but it's not what i wantet. As you call a script (bash) you can prepare the parameters in any script language.
Hope that helps.
For me it worked only with # prefix before channel id
I had some trouble with this after upgrading to a supergroup. The chat_id was updated and it was a bit harder to find this new id.
In the end I solved this with this by following this comment: https://stackoverflow.com/a/56078309/14213187
If you use a username, it does not require any prefix. That means the following are incorrect:
https://t.me/vahid_esmaily_ie
t.me/vahid_esmaily_ie
And this is the correct case:
vahid_esmaily_ie
If you want to use a bot message to the channel, you can refer step here
Steps:
Create a Telegram public channel
Create a Telegram BOT (for example x_bot) via BotFather
Set the x_bot as an administrator in your channel
the chat_id is #x_bot, it's a part of https://t.me/x_bot that does not add your channel name.
Telegram bots can't send messages to user, if that user hasn't started conversation with bot yet, or bot is not present in chat (if it's a group chat). This issue is not related to the library, this is simply Telegram restriction, so that bots can't spam users without their permission.
you need to first send a message to the bot before the bot can send messages to you.

Categories