Adding CR LF to a Teams chat message programmatically - microsoft-teams

I have an application that sends chat message like so:
const currentUserId: any = await msGraphProvider.getCurrentUserId();
const userIdToSendMessage: any = await msGraphProvider.getUserId(dpcRequest.SendQuestionsTo.EMail);
const chatOfUser: any = await msGraphProvider.createUsersChat(userIdToSendMessage, currentUserId);
const result: any = await msGraphProvider.sendMessage(chatOfUser, text);
the text variable should have few new lines in it but anything I send is disregarded.
I tried but it is not HTML
also \r\n\r\n , \n but it is ignored too
Any Idea how to format new lines?
Thanks

A simple text message like this (as opposed to, say, an Adaptive Card message) actually uses markdown, just fyi - you can see more at https://support.microsoft.com/en-us/office/use-markdown-formatting-in-teams-4d10bd65-55e2-4b2d-a1f3-2bebdcd2c772
That doesn't help you though, it's just fyi. For newline, this might be relevant: How to insert newline into MS Teams markdown?

Related

how to transfer body.getAsync plain text with \n

I'm getting outlook email content with
Office.context.mailbox.item.body.getAsync("text", async function callback(result) {})
and the result it returned is in plain text format with no line breaks info, I want to know if there is any way to get it with \n ?
You can get the HTML string which represents the message body instead.

Is there a way to check if the attachment send is either a image or video in discord.py?

I am making a bot that converts media.xxxx.net into cdn.xxxx.com links. This works perfectly for videos but as a side effect, it also converts images which is an undesirable effect.
One way would explicitly tell the bot which formats (mp4, MOV etc) to convert and leave the other formats but considering the amount of formats there are for video file, I think it is necessary but I can't come up with any other way.
Here is the code I wrote
if message.content.startswith('https://media') and not message.content.endswith('jpg') and not message.content.endswith('png') and not message.content.endswith('gif') and not message.content.endswith('webp'):
message.content.replace('media', 'cdn').replace('net', 'com')
await message.delete()
Here I was trying to make the bot ignore the most common picture formats but it is just looking like a mess as I add more and more formats.
You can check for the file type of the attachments using the content_type attribute and if they are an image you can tell it to ignore it.
for att in message.attachements:
if att.content_type.startswith("video"):
new_url = message.content.replace('media', 'cdn').replace('net', 'com')
await message.channel.send(new_url)
await message.delete()
Instead of checking for if the URL does not ends with the image format, check if the URL ends with a video format.
message_text = message.content
if message_text.find('https://media.discordapp.net') != -1 and message_text.find('.mp4') != -1:
new_url = message_text.replace('https://media.discordapp.net','https://cdn.discordapp.com')
await message.channel.send(new_url)
await message.delete()
To make my code cleaner, I made a list and added all the possible formats in there, it is basically the same LOGIC as my previous way but it is a lot cleaner and more manageable.
formats=['png', 'jpg', 'gif' , 'webp', 'jpeg', 'jpg' , 'jpeg' ,'jfif' ,'pjpeg' , 'pjp', 'svg', 'bmp']
msg = message.content
if message.content.startswith('https://media'):
if any(word in msg for word in formats):
return
So it will check for the word in the link, if it finds any one of the formats (jpg, png), it will return instead of running the next piece of code

Replying to a message with a embed (Discord.py)

i'm having a problem where i can't reply to a message with an embed.
is this even possible? I'm guessing it is and im just doing it horribly wrong.
My error - TypeError: object method can't be used in 'await' expression
if message.content.startswith('!test'):
await message.reply
embedVar = discord.Embed(description=".order", color=0x7289da)
embedVar.add_field(name='test', value='test')
await message.channel.send(embed=embedVar)
Looking at the discord.py API Reference, it should be possible. Note that Message.reply() is a method, not an attribute. The documentation describes the method:
A shortcut method to abc.Messageable.send() to reply to the Message.
The method takes positional argument content and keywords arguments kwargs, which correspond to the keyword arguments of the TextChannel.send() method. Since embed is one of the keyword arguments, it is one of reply() as well.
if message.content.startswith('!test'):
embed = discord.Embed(description=".order", color=0x7289da)
embed.add_field(name="test", value="test")
await message.reply(embed)
On a side note, I suggest using the discord.ext.commands framework instead of using on_message() and string methods.
You are not using the reply function correctly. You cannot have it by itself, as you have done with await message.reply in your code.
# on_message event, the way you have done it
if message.content.startswith('!test'):
await message.reply("This is a test!")
# or if you prefer the commands extension (recommended)
#bot.command()
async def test(ctx):
await ctx.reply("This is a test!")
# you can also make it so it doesn't mention the author
await ctx.reply("This is a test!", mention_author=False)
# Note that both of the code in the commands example can be used in your on_message event
# as long as you remember to change ctx to message
The above code has been tested as seen in the image below.
As for replying with embeds, I have found that if the bot is only replying with an embed, it may not mention the author.
embed = discord.Embed(description="This is a test", color=0x123456)
await message.reply(embed=embed)
The above code works as seen in the image below.
Try this :
embed=discord.Embed(title="Tile",
description="Desc", color=0x00ff00)
embed.add_field(name="Fiel1", value="hi", inline=False)
embed.add_field(name="Field2", value="hi2", inline=False)
await self.bot.say(embed=embed)
`

how to make bot be able to show link for any emoji from any server

so this command works and all but...
#commands.command()
async def se(self, ctx, emoji: discord.Emoji):
await ctx.send(f"**Name:**Illdo this later **Link:**{emoji.url}")
it only works for the emojis from the server that the bot is in.
does anyone know how to make it be able to get the link for any servers emojis? even if the bot isnt in it
and if you wanna, also i need help with the showing the name of the emoji
the kind of thing i am going for is
thank you!
You need to get the id of the emoji and make the url itself
You can get its id by some split() and also you need to check if its animated so we can use .gif and .png accordingly
Below is the code:
#commands.command()
async def se(self, ctx, *, msg):
_id = msg.split(":") # split by ":"
if "<a" == _id[0]: # animated emojis structure <a:name:id>
ext = "gif"
else:
ext = "png" # normal emojis structure <name:id>
e_id = _id[2].split(">")[0].strip()# get the id
# url for a emoji is like this, try yourself if you want to check by manually copying any emoji's url
url = f"https://cdn.discordapp.com/emojis/{e_id}.{ext}"
await ctx.send(f"**Name**: :{_id[1]}: **Link**: {url}")

how to store the user input in a parameter in dialogflow

I have a Intent where I have a parameter called age and its value I want it to be the user input.
And in the response I would like to responde with the same input as user given ex:
User Input: Hello, haw are you.
Bot: You said: Hello, haw are you.
So I would need the user input first to store in a parameter and from the Text Response section I can call the parameter but I just don't know how to catch the user input at this moment!
So Text response would be like: You said: $input.
Assuming that you are using Javascript, you can find many useful details here.
Depending on the way you get the input, the solution is totally different.
For instance, in this link you get the input via a click (this.button.addEventListener("click", this.handleClick.bind(this))) or in the following code you get the input as a query.
import {ApiAiClient} from "api-ai-javascript";
import {IRequestOptions, IServerResponse, ApiAiConstants} from "api-ai-javascript/ApiAiClient"
const client = new ApiAiClient({accessToken: 'YOUR_ACCESS_TOKEN'})
queryInput.addEventListener("keydown", queryInputKeyDown);
.textRequest('Hello!')
.then((response) => {console.log(queryInput.value);})
Thus, in the above example, you can use:
value = queryInput.value
you can try it in Inline Editor Fulfillment
function userInput(agent) {
let user_input = agent.query;
//test it
agent.add(user_input);
}

Resources